1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
| // 入门Java代码练习与记录 // author: tommonkey
// if...else if的使用 /* public class test { public static void main(String[] args){ int year = 2001; if(year==2000) System.out.println("year is :"+year); else if (year==2001) System.out.println("over!"); } } */
// switch...case的使用 /* public class test { public static void main(String[] args){ int month = 8; switch(month) { case 1:System.out.println("1");break; case 2:System.out.println("2");break; case 3:System.out.println("3");break; case 8:System.out.println("8");break; } } } */
// while循环的使用 /* public class test { public static void main(String[] args){ int i = 1; long sun = 0; while (i<100){ sun+=i; i++; } System.out.println(sun); } } */
// for循环的使用 /* public class test { public static void main(String[] args){ int sum = 0; for (int i=1;i<100;i++){ sum+=i; } System.out.println(sum); } } */
// do...while循环的使用 /* public class test { public static void main(String[] agrs){ int i = 1; do { i++; }while (i<100); System.out.println(i); } } */
//方法的重载实列 /* public class test { public static void main(String[] args) { int n1 = 5, n2 = 10; char c1 = 'a', c2 = 'b'; float f1 = 0.5f, f2 = 0.1f; System.out.println(add(n1, n2)); System.out.println(add(c1, c2)); System.out.println((add(f1, f2))); }
static int add(int a,int b){ return a+b; }
static double add(double a,double b){ return a+b; } } */
// 对象数组:对象可为数组的元素 /* class A{ int a,b; // 两个构析方法 A(){ a=0;b=0; } A(int i,int m){ a=i;b=m; } void setA(int i,int m){ a=i;b=m; } }
class test{ public static void main(String[] args){ A[] a=new A[3]; // 创建对象数组,元素数量为3个,每个元素都是A的对象 a[0]=new A(); // 初始化数组 a[1]=new A(1,2); a[2]=new A(); System.out.println(a[0].a+a[0].b); System.out.println(a[1].a+a[1].b); a[2].setA(10,11); System.out.println(a[2].a+a[2].b); } } */
// 对象作为方法的参数 /* class A{ int a=10; void method1(A a){ //这里传入一个A类的一个对象进去 System.out.println((a.a)); a.a+=5; System.out.println(a.a); } }
class test{ public static void main(String[] args){ A ao=new A(); // 这里创建了一个A类的一个对象 System.out.println(ao.a); ao.method1(ao); // 传入 System.out.println(ao.a); } } */
// 对象作为方法的返回值 /* class A{ int a; A(int i){ a=i; } A method1(){ // 类名+方法名():意思是这个方法的返回类型是某个类的实例 A a1=new A(a=10); return a1; // 返回A类的对象:a1 } }
class test{ public static void main(String[] args){ A a2=new A(1); A a3; System.out.println(a2.a); a3=a2.method1(); System.out.println(a3.a); a3=a3.method1(); System.out.println(a3.a); } } */
// 接口&实现堆栈和队列 /* interface Access{ void put(char ch); char get(); }
// 栈: class Stack implements Access{ private char[] array=new char[80]; private int top=0;
public void put(char ch){ array[top++]=ch; }
public char get(){ return array[--top]; } }
// 队列: class Quene implements Access{ private char[] array=new char[80]; private int readin=0,readout=0;
public void put(char ch){ array[readin++]=ch; } public char get(){ return array[readout++]; } }
class test{ public static void main(String[] args){ Stack s=new Stack(); s.put('1'); s.put('2'); s.put('3'); s.put('4'); s.put('5'); System.out.println(s.get()); System.out.println(s.get()); System.out.println(s.get()); System.out.println(s.get()); System.out.println(s.get());
Quene q=new Quene(); q.put('1'); q.put('2'); q.put('3'); q.put('4'); q.put('5'); System.out.println(q.get()); System.out.println(q.get()); System.out.println(q.get()); System.out.println(q.get()); System.out.println(q.get()); } } */
// 字符串常量的常用操作,String类,该类创建的对象是不允许改变的,创建:String s = new String("1111"); // StringBuffer是创建字符串变量! /* class test{ public static void main(String[] args){
String a = new String("world!"); System.out.println(a);
String s = "what's your name?"; System.out.println("字符串长度为:"+s.length()); System.out.println("字符串中是否含有y:"+s.indexOf('y')); System.out.println("从第五位开始向后查询,是否存在y:"+s.indexOf('y',5)); System.out.println("是否存在name:"+s.indexOf("come")); System.out.println("是否相等:"+s.equals("what's wrong!")); System.out.println("是否相等:"+s.equalsIgnoreCase("what's wrong!")); System.out.println("截取字符串:"+s.substring(0,5)); System.out.println("截取指定位置后面的所有字符串:"+s.substring(6)); System.out.println("拼接字符串:"+s.concat("babiq!")); System.out.println(s.charAt(5)); System.out.println("字符串是否以what开头:"+s.startsWith("what")); } } */
// StringBuffer类的一些方法练习 // toString():将字符串变量转化为字符串常量,所有再输出前要提前使用此方法转换 class test{ public static void main(String[] args){ String str="beautiful"; StringBuffer s1=new StringBuffer(); StringBuffer s2=new StringBuffer(str);
System.out.println("s1的长度:"+s1.length()); System.out.println("s1的可容纳字数:"+s1.capacity()); System.out.println("字符串变量转变为字符串常量:/"+s1.toString()); System.out.println("s2可容纳字符长度:"+s2.length()); s2.setLength(10); System.out.println("设置s2长度后,现在可容纳长度数:"+s2.length()); System.out.println("变量转常量后打印输出:"+s2.toString()); System.out.println("输出指定位置字符串:"+s2.charAt(5)); s2.setCharAt(5,'A'); // 替换指定位置的字符 System.out.println("输出修改后的字符串:"+s2.toString()); s2.append("abcdef"); System.out.println(s2.toString()); s2.insert(5,'B'); System.out.println("插入后的s2:"+s2.toString()); } }
|