1 package 课堂test; 2 3 import java.lang.reflect.Array; 4 import java.util.Arrays; 5 6 public class 所学内容汇总 { 7 static int a2 = 5; //这是一个成员变量 8 public static void main(String[] args) { 9 System.out.println("输出一句简单的话"); //开始接触java所学的第一堂课,输出。 10 11 //基本的数据类型,4种类型,8种数据 12 byte a = 8; //数据类型中8位等于一个字节 13 short b = 16; 14 int c = 32; float f = 32; //int和float位数是一样的,注意float型如果赋值为小数后面必须加 f ,赋值为整数的时候可以不加 15 long d = 64; double g = 64; //long和double位数是一样的,double后面不需要加d 16 char e = 16; 17 boolean h; //布尔型数据只有两个值true or false 18 19 //常用的转义字符,课本P43 20 char c1 = '\\'; char c2 = '\u2605'; 21 System.out.println(c1+"\n"+c2+" \' "); 22 System.out.println(0); 23 //标识符和关键字,java是严格区分大小写的,标识符规则是,不能用关键字做标识符,不能以数字开头,组成部分可以是字母,下划线,$ 24 //常用关键字,课本P44。 25 //声明常量 26 final int a1 = 1; //只能被赋值一次,并且不能被更改 27 //局部变量与成员变量,第4行程序为静态成员变量,在整个类中都可以使用,第22行为局部变量,只能在方法体中使用。 28 //算数运算符 29 System.out.println("a+b求和"+(a + b)); 30 System.out.println("a+b="+a + b); //加法运算如果不加括号+就变成了连接符 31 System.out.println("b-a求差"+(b - a)); 32 System.out.println("a,b求积"+a * b); 33 System.out.println("d,a求商"+d / a); 34 System.out.println("d对5取余"+d % a2); 35 System.out.println(1); 36 //比较运算符,比较运算符的返回结果全部为布尔型数据 37 System.out.println(a > b); 38 System.out.println(a < b); 39 System.out.println(a == b); 40 System.out.println(a <= b); 41 System.out.println(a >= b); 42 System.out.println(a != b); 43 System.out.println(2); 44 //逻辑运算符(与&),(或|),(非!),操作元必须是布尔型的,所以返回值也是布尔型的,注意有一个短路写法(与&&),(或||)★★★ 45 //运算符的优先级,课本P55。 46 //数据类型的转换 47 int a3 = a + (int)f; //在标识符前面加个括号里面写上要转成的类型 48 49 //复合语句,又称块语句,以一个区域为单位 50 51 //条件语句和循环语句 52 if(a > 0) { 53 System.out.println("a是大于0的"); 54 } 55 System.out.println(3); 56 57 if(a < 0) { 58 System.out.println("a是小于0的"); 59 } 60 else { 61 System.out.println("a是大于0的"); 62 } 63 System.out.println(4); 64 65 if(a > 0) { 66 System.out.println("a是大于0的"); 67 } 68 else if(a == 0) { 69 System.out.println("a是等于0的"); 70 } 71 else { 72 System.out.println("a是小于0的"); 73 } 74 System.out.println(5); 75 76 //switch case多分支语句 77 switch(a + b) //switch里面可以是一个字符串,也可以是任何能表达的一个条件式 78 { 79 case 1: //case后面的值必须为常量 80 System.out.println("haha"); 81 break; //如果不加break,系统会执行这个的语句块 82 case 2: 83 System.out.println("hehe"); 84 break; 85 default: 86 System.out.println("hoho"); 87 break; 88 } 89 System.out.println(6); 90 91 while(a < 50) 92 { 93 System.out.print(a + " "); 94 a++; 95 a++; 96 } 97 System.out.println(); 98 System.out.println(7); 99 100 do 101 { 102 System.out.print(a2 + " "); 103 a2++; 104 a2++; 105 a2++; 106 a2++; 107 } 108 while(a2 < 50); 109 System.out.println(); 110 System.out.println(8); 111 112 //for循环,计算1加到100的值 113 int a6 = 0; 114 for(int a4 = 1; a4<=100; a4++) 115 { 116 a6 = a6 + a4; 117 } 118 System.out.println("1+2+...+99+100="+a6); 119 120 //字符串,字符串的定义 121 String s; 122 //两种定义方法 123 s = "我们都是好学生"; 124 125 char[] ss = {'我', '们', '都', '是', '好', '学', '生'}; //这句本身就是在定义一个字符串 126 s = new String(ss); 127 System.out.println(s); 128 s = new String(ss, 2, 5); //代表的意义是从索引值为2的字符开始一共5个字符组成的字符串 129 System.out.println(s); 130 System.out.println(9); 131 String String = "StringString"; //String 可以做变量名 132 System.out.println(String); 133 //字符串的操作 134 //1,获取字符串的长度 135 String str = "What's tHe Matter? "; 136 int b1 = str.length(); 137 //2,查找字符串 138 int b2 = str.indexOf("t"); 139 int b3 = str.lastIndexOf("t"); 140 int b4 = str.lastIndexOf(""); //可以作为检验字符串长度的方法 141 System.out.println("b1的值为"+b1+"\nb2的值为"+b2+"\nb3的值为"+b3+"\nb4的值为"+b4); 142 System.out.println(str.charAt(9)+" "+str.charAt(8)); //输出指定索引位置的字符 143 //截取字符串 144 String str1 = str.substring(4); 145 String str2 = str.substring(5, 11); 146 System.out.println(str.trim().length()+"\n"+str1+"\n"+str2); 147 System.out.println(10); 148 //字符串的替换 149 String str3 = str.replace("what", "How"); //替换之后要有一个变量来装下替换后的字符串 150 //判断字符串的开始和结尾,注意返回值的类型为布尔型的 151 boolean b5 = str.startsWith("W"); 152 boolean b6 = str.endsWith("?"); 153 System.out.println(str3+"\n"+b5+"\n"+b6); 154 System.out.println(11); 155 156 //判断字符串是否相等 157 String str4 = "How are you"; 158 String str5 = "How are you"; 159 String str6 = new String("How are you"); 160 String str7 = new String("How are you"); 161 boolean x = str4==str5; 162 boolean y = str6==str7; 163 boolean z = str6.equals(str7); 164 System.out.println("x的值为"+x); 165 System.out.println("y的值为"+y); 166 System.out.println("z的值为"+z); 167 String str8 = str4.toUpperCase(); //也是需要一个新的字符串变量来装下新的编程大写的字符串 168 String str9 = str.toLowerCase(); 169 System.out.println(str8+"\n"+str9); 170 //字符串分割 171 String[] str0 = str.split("t"); //字符串分割后就变成了一个字符型数组,注意用新的变量成装的时候加"[]" 172 for(int s1 = 0; s1 < str0.length; s1++) //for语句循环遍历 173 { 174 System.out.print(str0[s1]+" "); 175 } 176 System.out.println(); 177 System.out.println(12); 178 for(String s2 : str0) //foreach循环遍历 179 { 180 System.out.print(s2+" "); 181 } 182 System.out.println(); 183 //格式化字符串,课本P91。 184 //字符串生成器 StringBuilder 185 StringBuilder sb = new StringBuilder(); 186 sb.append("q"); 187 sb.append('w'); 188 sb.append(4); 189 sb.append(true); 190 sb.append(3.1314); 191 System.out.println(sb); 192 sb.insert(2, "X"); //在这个索引值位置的前面插入字符 193 System.out.println(sb); 194 sb.delete(2, 6); 195 System.out.println(sb); 196 197 //一维数组 198 //创建数组是一个数据元素类型和一个[] 199 //分别定义和初始化一个数组 200 int[] arr = new int[5]; //此为定义一个数组,必须有的是数组元素的数据类型和数组的长度 201 int[] arr1 = {16, 11, 81, 10, 21, 13, 41, 25}; 202 int[] arr2 = new int[] {12, 23, 34, 45, 56}; //此为两个数组的赋值,记住一定要加上[] 203 arr1[2] = 7; 204 //一维数组的遍历 205 for(int c3 = 0; c3 < arr1.length; c3++) 206 { 207 System.out.print(arr1[c3]+" "); 208 } 209 System.out.println(); 210 //foreach语句的遍历 211 for(int c4 : arr2) 212 { 213 System.out.print(c4+" "); 214 } 215 System.out.println(); 216 System.out.println(13); 217 //声明二维数组 218 int[][] arr3; 219 int arr4[][]; 220 //为二维数组赋值,也可以只赋值前面几行,但是不能只赋值后面的列。 221 int[][] arr5 = new int[3][]; 222 int[][] arr6 = new int[3][5]; 223 //不规则的二维数组 224 int[][] arr7 = {{12, 34, 5}, {34, 7, 89, 45}, {29}}; 225 arr7[1][1] = 24; //直接给一个二维数组的一个元素赋值,这个办法同样适用于一维数组 226 //遍历二维数组 227 for(int c5 = 0; c5 <arr7.length; c5++) 228 { 229 for(int c6 = 0; c6<arr7[c5].length; c6++) 230 { 231 System.out.print(arr7[c5][c6]+" "); 232 } 233 System.out.println(); 234 } 235 System.out.println(13); 236 //foreach遍历二维数组 237 for(int[] c7 : arr7) 238 { 239 for(int c8 : c7) 240 { 241 System.out.print(c8+" "); //课本P108,有疑问 242 } 243 System.out.println(); 244 } 245 //数组的操作,填充,替换,复制,排序 246 Arrays.fill(arr, 3); 247 for(int c3 = 0; c3 < arr.length; c3++) 248 { 249 System.out.print(arr[c3]+" "); 250 } 251 System.out.println(); 252 Arrays.fill(arr, 2, 5, 8); 253 for(int c3 = 0; c3 < arr.length; c3++) 254 { 255 System.out.print(arr[c3]+" "); 256 } 257 System.out.println(); 258 Arrays.sort(arr1); 259 for(int c3 : arr1) 260 { 261 System.out.print(c3+" "); 262 } 263 System.out.println(); 264 int[] arr8 = new int[4]; 265 arr8 = Arrays.copyOf(arr1, arr8.length); //复制操作的返回值为一个新的数组,并不是一个方法,所以也是需要赋值给别的变量 266 for(int c3 : arr8) 267 { 268 System.out.print(c3+" "); 269 } 270 System.out.println(); 271 arr8 = Arrays.copyOf(arr1, 12); //复制操作的返回值为一个新的数组,并不是一个方法,所以也是需要赋值给别的变量 272 for(int c3 : arr8) 273 { 274 System.out.print(c3+" "); 275 } 276 System.out.println(); 277 int d1 = Arrays.binarySearch(arr1, 41); //道理同上,也是需要赋值给一个变量 278 int d2 = Arrays.binarySearch(arr1, 1, 7, 25); 279 System.out.println(d1+" "+d2); 280 } 281 282 }
1 package 汇总; 2 3 public class lei { 4 public lei() //构造方法必须和类名相同 5 { 6 System.out.println("选择排序算法"); 7 } 8 public int n; 9 public int i = 0; 10 public int j = 0; 11 public void maopaoPX(int[] arr) //类中的方法函数,定义成员方法 12 { 13 System.out.println("冒泡排序"); 14 for(j = 0; j <= arr.length; j++) 15 { 16 for(i = 1; i <= arr.length - j - 1; i++) 17 { 18 if(arr[i - 1] < arr[i]) 19 { 20 n = arr[i - 1]; 21 arr[i - 1] = arr[i]; 22 arr[i] = n; 23 } 24 } 25 } 26 for(int m : arr) 27 { 28 System.out.print(m+" "); 29 } 30 System.out.println(); 31 } 32 public void fanzhuanPX(int[] arr) 33 { 34 System.out.println("反转排序"); 35 for(i = 0; i < arr.length/2; i++) 36 { 37 n = arr[arr.length - i - 1]; 38 arr[arr.length - i - 1] = arr[i]; 39 arr[i] = n; 40 } 41 for(int m : arr) 42 { 43 System.out.print(m+" "); 44 } 45 System.out.println(); 46 } 47 48 }
1 package 汇总; 2 3 public class testlei { 4 5 public static void main(String[] args) { 6 int[] arr = new int[]{12, 47, 55, 22, 10, 11, 3, 43}; 7 int[] arr1 = new int[]{13, 56, 23, 33, 41, 42, 5, 2, 9}; 8 int[] arr2 = new int[]{2, 14, 3, 7, 12, 19}; 9 //arr[] = {12, 22, 10, 11, 45, 98, 43}; 10 lei we = new lei(); 11 we.maopaoPX(arr); 12 we.maopaoPX(arr1); 13 we.fanzhuanPX(arr2); 14 } 15 }
eclipse显示结果:
输出一句简单的话
\
★ '
0
a+b求和24
a+b=816
b-a求差8
a,b求积128
d,a求商8
d对5取余4
1
false
true
false
true
false
true
2
a是大于0的
3
a是大于0的
4
a是大于0的
5
hoho
6
8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
7
5 9 13 17 21 25 29 33 37 41 45 49
8
1+2+...+99+100=5050
我们都是好学生
都是好学生
9
StringString
b1的值为29
b2的值为3
b3的值为14
b4的值为29
e H
18
's tHe Matter?
s tHe
10
What's tHe Matter?
true
false
11
x的值为true
y的值为false
z的值为true
HOW ARE YOU
what's the matter?
Wha 's He Ma er?
12
Wha 's He Ma er?
qw4true3.1314
qwX4true3.1314
qwue3.1314
16 11 7 10 21 13 41 25
12 23 34 45 56
13
12 34 5
34 24 89 45
29
13
12 34 5
34 24 89 45
29
3 3 3 3 3
3 3 8 8 8
7 10 11 13 16 21 25 41
7 10 11 13
7 10 11 13 16 21 25 41 0 0 0 0
7 6
选择排序算法
冒泡排序
55 47 43 22 12 11 10 3
冒泡排序
56 42 41 33 23 13 9 5 2
反转排序
19 12 7 3 14 2
一些之前上课的时候没留心的问题,
1,复合语句,又称块语句,以一个区域为单位。
2,char[] ss = {'我', '们', '都', '是', '好', '学', '生'}; 这句本身就是在定义一个字符串。
3,在对字符串进行操作的时候,比如替换,截取,更改大小写等操作的时候,只是创建了一个新的字符串,对原字符串并不会有改动。并且也需要将新的字符串赋值给一个变量来保存下来。
4,String不是一个关键字,可以用来作为标识符。
5,字符串生成器中可以添加boolean型的字符,就是true和false,添加进去之后就变成字符串了
6,int b1 = str.length(); s1 < str0.length;一个是方法,一个是属性,这里代表的含义是一样的,但得到最后结果的方法不同
7,二维数组赋值,可以只赋值前面几行,但是不能只赋值后面的列。比如,int arr[][] = new int[3][];是可以的,int arr[][] = new int[][3]不行;
8,arr7[1][1] = 24;直接给一个二维数组的一个元素赋值,这个办法同样适用于一维数组
浙公网安备 33010602011771号