编辑
package cn.tedu.method; public class Demo1Method { public static void main(String[] args) { method1(); } public static void method1(){ System.out.println("method1()方法执行了"); } }
编辑
package cn.tedu.method; public class Demo2Method { public static void main(String[] args) { int area ; area=method2(2,4); System.out.println(area); } public static int method2(int w,int l){ return w*l; } }
编辑
编辑
编辑
编辑
编辑
package cn.tedu.array; public class Demo1array { public static void main(String[] args) { //静态初始化:程序员指定元素值,系统决定数组长度 int[] arr1 = new int[]{1,2,3} ; //等价于 int[] arr1 = {1,2,3} ; //动态初始化:程序员指定长度,系统按照类型分配元素默认值 double[] arr2 = new double[3];//没有如上的等价的简化写法 } }
编辑
package cn.tedu.array; public class Demo2array { public static void main(String[] args) { int[] arr1 = new int[]{1,2,3} ; System.out.println(arr1[0]); System.out.println(arr1.length); double[] arr2 = new double[3];//没有如上的等价的简化写法 arr2[0]=12.9; System.out.println(arr2[0]); } }
编辑
编辑
编辑
编辑
package cn.tedu.arraymethod; import java.util.Arrays; /** * 1.Arrays.toString( 数组变量 ): 将数组中的所有元素拼接为一个字符串并返回 * 2.copyOf( 原数组, 新数组的长度 ): 将原数组中的所有元素, 复制到一个指定长度的新数组中 * 1) 如果原数组长度是8: 10, 20, 30, 40, 50, 60, 70, 80, 新数组长度也是8, 就是将 * 原数组中的所有元素复制到新数组中 * 2) 如果原数组长度是8, 新数组长度是10, 将原数组中的所有元素复制到新数组中, 剩余的两个 * 位置, 会填充默认值 * 3) 如果原数组长度是8, 新数组长度是6, 将原数组中的前6个元素复制到新数组中, 剩余的两个 * 元素就被丢弃了 * 3.sort( 数组变量 ): 对数组中的元素按照自然顺序排序, 只能从小到大排序 */ public class Demo2copyof { public static void main(String[] args) { //1.测试toString()方法 int[] arr1 = new int[]{ 88, 100, 55, 77, 99 }; //如果直接输出数组变量, 输出的是数组对象在内存中的地址,这个地址对我们来说没什么意义 //System.out.println( "arr1=" + arr1 ); //arr1=[I@1b6d3586 //String s1 = Arrays.toString( arr1 ); //"[88, 100, 55, 77, 99]" //System.out.println( "arr1=" + s1 ); System.out.println( Arrays.toString( arr1 ) ); System.out.println( "-----分割线-----" ); //2.测试copyOf()方法 int[] arr2 = new int[]{10, 20, 30, 40, 50, 60, 70, 80}; System.out.println( "原数组中的所有元素为: " + Arrays.toString( arr2 ) ); //第1种用法: 如果原数组长度是8, 新数组长度也是8, 就是将原数组中的所有元素复制到新数组中 int[] newArray1 = Arrays.copyOf( arr2, 8 ); System.out.println( "新数组中的所有元素为: " + Arrays.toString( newArray1 ) ); //第2种用法: 如果原数组长度是8, 新数组长度是10, 将原数组中的所有元素复制到新数组中, // 剩余的两个位置, 会填充默认值 int[] newArray2 = Arrays.copyOf( arr2, 10 ); System.out.println( "新数组中的所有元素为: " + Arrays.toString( newArray2 ) ); //第3种用法: 如果原数组长度是8, 新数组长度是6, 将原数组中的前6个元素复制到新数组中, // 剩余的两个元素就被丢弃了 int[] newArray3 = Arrays.copyOf( arr2, 6 ); System.out.println( "新数组中的所有元素为: " + Arrays.toString( newArray3 ) ); System.out.println( "-----分割线-----" ); //3.测试sort()方法, 该方法就是对数组本身排序, 不会返回新数组 int[] arr3 = new int[]{ 88, 100, 55, 77, 99 }; System.out.println( "arr3=" + Arrays.toString( arr3 ) ); Arrays.sort( arr3 ); System.out.println( "arr3=" + Arrays.toString( arr3 ) );//[55, 77, 88, 99, 100] char[] chs = { 'e', 'b', 'a', 'd', 'c' }; System.out.println( "chs=" + Arrays.toString( chs ) ); //[e, b, a, d, c] Arrays.sort( chs ); System.out.println( "chs=" + Arrays.toString( chs ) ); //[a, b, c, d, e] String[] strs = { "eee", "abb", "aaa", "ddd", "ccc" }; System.out.println( "strs=" + Arrays.toString( strs ) ); //[eee, abb, aaa, ddd, ccc] Arrays.sort( strs ); System.out.println( "strs=" + Arrays.toString( strs ) ); //[aaa, abb, ccc, ddd, eee] } }
编辑
编辑
编辑
编辑
编辑
编辑
编辑