java零基础知识回顾(for与while的区别,以及for的一般用法) 1.for与while的区别 for和while都可以表示循环,学习java零基础的教程,了解到for循环定义的变量值只在for循环内部有效,随着for循环的结束,变量将会在内存中被释放掉; public class Sentence { public static void main(String[] args) { for(int x=0;x<3;x++){ System.out.println(x); } System.out.println(x); // 编译失败,for循环结束后x在内存中被释放掉 } } while循环定义的变量在while循环的外部有效,不随着while循环的结束被释放 可以正常的编译并运行处结果。 public class Sentence { public static void main(String[] args) { int y =0; while(y<3){ System.out.println(y); y++; } System.out.println(y); //正常编译并可以运行出结果 } } for的一般用法 1.嵌套for循环 public class Sentence { public static void main(String[] args) { for(int x=0;x<5;x++){ //外循环控制行数 for(int y=x;y<=4;y++){ //内循环控制每一行中元素的个数 System.out.print("*"); } System.out.println(); } } } // 打印结果为5行,第一行为5个*,依次递减,第5行为1个* public class Sentence { public static void main(String[] args) { for(int x=0;x<5;x++){ for(int y=0;y<=x;y++){ System.out.print("*"); } System.out.println(); } } } // 打印结果为5行,第一行为1个*,依次递增,第5行为5个* 2.打印九九乘法表 public class Sentence { public static void main(String[] args) { for(int x=1;x<10;x++) { for(int y=1;y<=x;y++){ System.out.print(y+"*"+x+"="+x*y+"\t"); // "\t"为制表符 } System.out.println(); } } } 3.数组 (1)获取数组最值的时候既可以初始化数组的任意一个元素,也可以初始化数组当中任意一个角标。 (2)数组排序 选择排序 特点:内循环结束一次,最值出现在头角标位置上 public static void sortArray1(int[] arr){ for(int x=0;xarr[y]){ int temp = arr[x]; arr[x] = arr[y]; arr[y]= temp; } } } } 冒泡排序 public static void sortArray2(int[] arr){ for(int x=0;x