java数组操作


package com.basic;
import java.util.Arrays;
class Student{
String name;
String age;
public Student(String name,String age){
this.name=name;
this.age=age;

}
public void operate(){
System.out.println("I graduated!");
}
public String toString(){
return name+":"+age;
}

}
public class ArrayT {

public void simpleT(){
String []ss=new String[]{"aaa","bbb"};
System.out.println(ss[0]);
//String []sss=new String[3]{"aaa","bbb","ccc"};error
String []s={"aa","bb"};
String []sc=s;
System.out.println(sc[1]);
String []su=new String[2];
System.out.println(su[1]);
}
public void arrays(){
int []a={1,5,4,3,2,6};
int []b=Arrays.copyOf(a,8);//8截取长度,超过用零填充
System.out.println(b[7]);
int []bb=Arrays.copyOfRange(a,3,5);//3截取长度
System.out.println(bb[1]);//5
int c[]=Arrays.copyOf(a, a.length);
System.out.println(Arrays.equals(a, c));//true
Arrays.sort(a);//对a升序排序
for(int o:a)
System.out.print(o+" ");

}
public void ObjectArray(){
Student st[]=new Student[2];
System.out.println(st[1]);
//st[1].operate();空指针
st[1]=new Student("zhaozhi","22");
System.out.println(st[1]);
}
public static void main(String[] args) {
ArrayT art=new ArrayT();
art.simpleT();
Student student=new Student("zhaozhi3758","22");
System.out.println(student);
art.ObjectArray();
System.out.println("--------------");
art.arrays();
System.out.println();
//数组合并
int a[]={1,2,3};
int b[]={4,5,6};
int c[]=new int[a.length+b.length-1];
System.arraycopy(a,0,c,0,a.length);
System.arraycopy(b,0,c,a.length-1,b.length);
for(int d:c){
System.out.print(d+"--");
}

}

}

你可能感兴趣的:(java基础)