1 publilc class Shuzu{ 2 public static void main(String ags[]){ 3 { 4 int a[]={1,2,3,4}; 5 char b[]={a,b,c,d}; 6 for(int i=0;i<a.length;i++){ 7 System.out.print(a[i]);//传统方法遍历数组a 8 System.out.println(""); 9 } 10 for(String s:b){ 11 System.out.print(" "+s);//非传统方式遍历数组b 12 } 13 } 14 }
1 import java.util.*; 2 public class Shuzu1{ 3 public static void main(String args[]){ 4 char a[]={'a','b','c','d'}; 5 String str=Arrays.toString(a); 6 System.out.println(a); 7 } 8 } 9 10 2、toString方法遍历
1 import java.util.*; 2 public class Shuzu2{ 3 public static void main(String args[]){ 4 int a[]={1,2,3,4},b[]; 5 b=a;//数组属于引用类型,所以数组b与数组a的引用相同 6 System.out.println(a==b);//true 7 System.out.println(Arrays.toSting(a)); 8 System.out.println(Arrays.toSting(b));//与上相同 9 } 10 }
1 import java.util.*; 2 public class Shuzu3{ 3 public static void main(String args[]){ 4 char a[]={'A','B','C','D','E','F'}; 5 char b[]={'1','2','3','4','5','6'}; 6 System.arraycopy(a,0,b,0,a.length);//a的数组复制到b数组 7 System.out.println(Arrays.toString(b));//与数组a一样 8 } 9 }
import java.util.*; public class Shuzu5{ public static void main(String args[]){ int a[]={1,2,3,4,5},b,c; b[]=Arrays.copyOf(a,8);//数组a只有5个元素,所以数组b的8位中后3位默认为0 System.out.println(Arrays.toString(a));//输出[1,2,3,4,5] System.out.println(Arrays.toString(b));//输出[1,2,3,4,5,0,0,0] c[]=Arrays.copyOfArray(a,3,5);// System/.out.println(Arrays.toString(c))//输出[4,5] } }
1 //Arrays类的sort方法实现快速排序 2 //Arrays.binarySearch(数组,要查找的值)实现二分查找,返回要查找的值在数组中的索引号,若没有找到,返回0 3 import java.util.*; 4 public class Shuzu5{ 5 public Static void mian(String args[]){ 6 Scanner scanner=new Scanner(System.in); 7 int a[]={12,34,9,23,45,6,45,90,123,19,34}; 8 Arrays.sort(a);//排序 9 System.out.println("输入整数,判断该整数是否在数组中:"); 10 while(scanner.hasIntNext()){ 11 int number=scanner.nextInt();//输入数组 12 int index=Arrays.binarySearch(a,number);//二分查找 13 if(index>0){ 14 System.out.println(number+"和数组a中索引为"+index+"的值相同"); 15 } 16 else System.out.println(number+"不与数组a的任何值相同"); 17 System.out.println("是否继续输入整数,输入任何非整数可结束"); 18 } 19 } 20 }
1 public class Shuzu6{ 2 public static void main(String args[]){ 3 Weekday x=Weekday.星期天; 4 if(x==x.Weekday.星期天){ 5 System.out.println(x); 6 System.out.println("今天我休息"); 7 } 8 } 9 } 10 11 //枚举enum 12 enum Weekday{ 13 星期一,星期二,星期三,星期四,星期五,星期六,星期天 14 }