publicclassDemo01{ //属性:变量 //类变量 static static String name = "张三";
//实例变量:从属于对象 int age = 13;
publicstaticvoidmain(String[] args){ //局部变量:必须声明和初始化值 char h = '中'; System.out.println(h); System.out.println(name); shout(); Demo01 demo01 = new Demo01(); System.out.println(demo01.age);
}
publicstaticvoidshout(){ //局部变量 String name = "王五"; System.out.println(name); } }
1.7自增和自减
1 2 3 4 5 6 7 8 9 10
publicclassDemo02{ publicstaticvoidmain(String[] args){ int a = 3; int b = a++;//执行完语句后,先给b赋值,再自增 int c = ++a;//执行完语句前,先自增,后给c赋值 System.out.println(c); System.out.println(b); System.out.println(a); } }
自减类似
1.8三元运算符
1 2 3 4 5 6 7 8 9 10 11 12
//三元运算符 publicclassDemo03{ publicstaticvoidmain(String[] args){ int num = 80; String all = num > 60 ? "及格": "不及格"; System.out.println(all);
publicstaticvoidmain(String[] args){ //创建一个扫描器对象,键盘接收数据 Scanner scanner = new Scanner(System.in); System.out.println("使用next方式接受:"); //判断用户有没有输入字符串 if (scanner.hasNext()){ String next = scanner.next();//程序等待用户输入完毕 System.out.println("输出的内容:"+next); } scanner.close();//释放资源 }
1 2 3 4 5 6 7 8 9 10
publicstaticvoidmain(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("使用nextLine方式接受:"); //判断用户有没有输入字符串 if (scanner.hasNextLine()){ String s = scanner.nextLine(); System.out.println("输出的内容"+s); } scanner.close(); }
1 2 3 4 5 6 7
publicstaticvoidmain(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("输入的内容是"); String s = scanner.nextLine(); System.out.println("输出的内容是"+s); scanner.close(); }
判断整数
1 2 3 4 5 6 7 8 9 10 11 12 13
publicstaticvoidmain(String[] args){ Scanner scanner = new Scanner(System.in); System.out.println("请输入整数:"); //判断是否是整数 if (scanner.hasNextInt()){ int next = scanner.nextInt(); System.out.println("请输入整数"+next); }else { System.out.println("你输入的不是整数"); } scanner.close(); }
输入多个数字,求输入数字总数和平均数,每输入一个数字回车确认,非数字来结束并输出执行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicstaticvoidmain(String[] args){ Scanner scanner = new Scanner(System.in); //数 double i=0 ; //和 double sum = 0; while (scanner.hasNextDouble()){ double next = scanner.nextDouble(); i = i +1; sum = sum + next;
//输出1~100的数字 publicclassDemo07{ publicstaticvoidmain(String[] args){ int i = 0; while (i<100){ i++; System.out.println(i); }
} }
1 2 3 4 5 6 7 8 9 10
//输出1+2+3+......+100 publicstaticvoidmain(String[] args){ int i = 0;//初始化条件 int sum = 0; while (i<=100){//判断条件 sum = sum + i;//循环条件 i++;//迭代 } System.out.println(sum); }
dowhile
1 2 3 4 5 6 7 8 9 10
publicstaticvoidmain(String[] args){ int i = 0; int sum = 0; do { sum = sum + i; i++; }while (i<=100); System.out.println(sum);
publicstaticvoidmain(String[] args){ int i =0; while (i<0){ System.out.println(i); i++; } System.out.println("=========="); do { System.out.println(i); i++; }while (i<0); }
2.5for循环
语法格式
for(初始化;布尔表达式判断;更新){代码语句}
1 2 3 4 5
publicstaticvoidmain(String[] args){ for (int i=0;i<=100;i++){ System.out.println(i); } }
案例:计算0到100之间的奇数和偶数的和
1 2 3 4 5 6 7 8 9 10 11 12 13
publicstaticvoidmain(String[] args){ int oddsum = 0;//奇数初始化 int evensum = 0;//偶数初始化 for (int i = 0; i <= 100; i++) { if (i%2 !=0){ oddsum = oddsum +i; }else { evensum = evensum + i; } } System.out.println("奇数的和为"+oddsum); System.out.println("偶数的和为"+evensum); }
用for循环输出1到1000之间被5整除的数,并且每行输出3个
1 2 3 4 5 6 7 8 9 10 11
publicstaticvoidmain(String[] args){ for (int i = 0; i < 1000; i++) { if (i%5==0){ System.out.print(i+"\t");//print表示不换行 \t表示tab } if (i%(5*3)==0){ System.out.println();//println表示换行 System.out.print("\n");//\n表示换行 } } }
打印九九乘法表
1 2 3 4 5 6 7 8 9 10 11 12 13
//1、我们先打印第一列 //2、我们把固定的1再用一个循环包起来 //3、去掉重复项 i<=j //4、调整样式 //内部for先执行完后才重新执行外面的 publicstaticvoidmain(String[] args){ for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(j+"*"+i+"="+(j*i)+"\t"); } System.out.println(); } }
增强for循环
1 2 3 4 5 6 7 8 9
publicclassDemo10{ publicstaticvoidmain(String[] args){ int numbers[] ={10,20,30,40,50}; for (int number : numbers) { System.out.println(number); } }
}
2.6break和continue
区别
break用于强行退出循环,不执行循环中剩余的语句
contunue用于终止某次循环,接着进行下一次是否执行循环的判定
1 2 3 4 5 6 7 8 9 10 11
publicstaticvoidmain(String[] args){ int i = 0;
while (i<=10){ System.out.println(i); i++; if (i == 5){ break; } } }
1 2 3 4 5 6 7 8 9 10 11 12
publicstaticvoidmain(String[] args){ int i = 0;
while (i<=10){ i++; if (i == 5){ continue; } System.out.println(i);
} }
3.7打印三角形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicstaticvoidmain(String[] args){ for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int j = 1; j < i; j++) { System.out.print("*"); } System.out.println();
} }
3.java方法
3.1什么是方法
Java中方法是语句的集合,他们在一起执行的一个功能
方法 包含于类或对象中
方法在程序中被创建,在其他地方被引用
1 2 3 4 5 6 7 8 9 10
publicstaticvoidmain(String[] args){ //实际调用传递给形式参数的参数 int s = sum(10,23);
publicstaticvoidmain(String[] args){ int max =max(12,23); System.out.println(max);
} publicstaticintmax(int a,int b){ int result =0; if (a==b){ System.out.println("a==b"); return0;//终止方法 } if ( a> b){ result = a; }else { result =b; } return result; }
publicstaticvoidmain(String[] args){ double max =max(20,10); System.out.println(max);
} publicstaticintmax(int a,int b){ int result = 0; if (a == b){ System.out.println("a==b"); return0; } if (a>b){ result = a; }else { result = b; } return result; } publicstaticdoublemax(double a, double b){ double result = 0; if (a>b){ result = a; }else{ result = b; } return result; }
publicstaticvoidmain(String[] args){ int[] a ={1,2,3,4,5,6,7,8}; //打印全部数组的元素 for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } System.out.println("========"); //打印全部数组元素的和 int num = 0; for (int i = 0; i < a.length; i++) { num = num + a[i]; } System.out.println(num); System.out.println("=========="); //查找数组中元素最大的值 int max = a[0]; for (int i = 1; i < a.length; i++) { if (a[i]>max){ max = a[i]; } } System.out.println(max); }
4.6二维数组
1 2 3 4 5 6 7 8 9 10
publicstaticvoidmain(String[] args){ int[][] num= {{1,2},{2,3},{3,4},{4,5}}; for (int i = 0; i < num.length; i++) { for (int j = 0; j < num[i].length; j++) { System.out.println(num[i][j]); }
publicclassTest{ publicstaticvoidmain(String[] args){ Student student = new Student(); //父类引用子类 Person person = new Student(); student.run(); person.run();
publicclassListTest1{ publicstaticvoidmain(String[] args){ List list = new ArrayList(); list.add(123); list.add(456); list.add(new String("tom"));
//方式一 iterator迭代器 Iterator iterator = list.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } System.out.println("================="); //方式二:增强for循环 for (Object o : list) { System.out.println(o); } System.out.println("================="); //方式三:普通for循环 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
常用方法:
增 add(Object obj)
删 remove(Object obj) / remove(int index)
改 set(int index. Object obj)
查 get(int index)
插 add(int index, Object obj)
长度 size()
遍历
方式一 iterator迭代器
方式二:增强for循环
普通for循环
6.6Set接口
Set集合实现类
Set接口:存储无序的、不可重复的数据
HashSet:作为Set接口的主要实现类,线程不安全,可以存储null值
LinkedHashSet:作为HashSet的子类;遍历其内部数据时,可以按照添加的顺序遍历
TreeSet:可以按照添加对象的指定属性,进行顺序
说明:Set接口中没有额外定义新的方法,使用的都是Collection中声明过的方法
Set接口:存储无序的、不可重复的数据
以HashSet为例说明:
无序性:不等于随机性。存储的数据在底层数组中并非按照数组索引的顺序添加,而实根据数据的哈希值决定的
不可重复性:保证添加的元素按照equals()判断时,不能返回true.即:相同的元素只能添加一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassSetTest{ publicstaticvoidmain(String[] args){ Set set = new HashSet(); set.add(123); set.add(456); set.add(123); set.add("aaa"); set.add("bbb");
Iterator iterator = set.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
@Override //按照姓名从小到大 publicintcompareTo(Object o){ if (o instanceof Person){ Person person = (Person)o; returnthis.name.compareTo(person.name); }else { thrownew RuntimeException("输入的类型不匹配"); }
} }
1 2 3 4 5 6 7 8 9 10 11 12 13
publicclassSetTree{ publicstaticvoidmain(String[] args){ Set set = new TreeSet(); set.add(new Person("bob",99)); set.add(new Person("jerry",15)); set.add(new Person("mike",16)); set.add(new Person("tom",17)); Iterator iterator = set.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
publicclassCollectionsTest{ publicstaticvoidmain(String[] args){ List list = new ArrayList(); list.add("13"); list.add("8"); list.add("99"); list.add("88"); list.add("100"); System.out.println(list); //反转 reverse() Collections.reverse(list); System.out.println(list); //随机 shuffle() Collections.shuffle(list); System.out.println(list); //升序 sort() Collections.sort(list); System.out.println(list); //swap(List,int,int) 指定集合index位置交换 Collections.swap(list,1,2); System.out.println(list);
} }
copy()
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassCollectionsTest1{ publicstaticvoidmain(String[] args){ List list = new ArrayList(); list.add("13"); list.add("8"); list.add("99"); list.add("88"); list.add("100"); List dest = Arrays.asList(new Object[list.size()]); System.out.println(dest.size()); Collections.copy(dest,list); System.out.println(dest); } }
7.泛型
使用泛型可以保证数据的安全,避免了不同类型同时出现
使用泛型避免了强转操作
在集合中使用泛型
List中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassGenericTest{ publicstaticvoidmain(String[] args){ List<Integer> list = new ArrayList<>(); list.add(56); list.add(44); list.add(22); list.add(55); for (Integer integer : list) { int score = integer; System.out.println(score); } System.out.println("========="); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } }
Map中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassGenericTest1{ publicstaticvoidmain(String[] args){ HashMap<String, Integer> map = new HashMap<>(); map.put("AA",80); map.put("BB",58); map.put("CC",66); map.put("DD",90); //泛型的嵌套 Set<Map.Entry<String, Integer>> set = map.entrySet(); Iterator<Map.Entry<String, Integer>> iterator = set.iterator(); while (iterator.hasNext()){ Map.Entry<String, Integer> next = iterator.next(); String key = next.getKey(); Integer value = next.getValue(); System.out.println(key+"====="+value); } } }