数组复制System.arraycopy()
rihiroshitekiMacBook-Air:test LiBo$ java ArrayTest
1
2
3
rihiroshitekiMacBook-Air:test LiBo$
rihiroshitekiMacBook-Air:test LiBo$
rihiroshitekiMacBook-Air:test LiBo$
rihiroshitekiMacBook-Air:test LiBo$ cat ArrayTest.java
class ArrayTest
{
public static void main(String[] args)
{
int[] num1=new int[] {1,2,3};
int[] num2=new int[3];
System.arraycopy(num1,0,num2,0,num1.length);
for (int i=0;i<num2.length;i++)
{
System.out.println(num2[i]);
}
}
}
数组排序:Arrays.sort()
rihiroshitekiMacBook-Air:test LiBo$ java ArrayTest
1
2
3
5
rihiroshitekiMacBook-Air:test LiBo$ cat ArrayTest.java
import java.util.Arrays;
class ArrayTest
{
public static void main(String[] args)
{
int[] num=new int[] {3,1,2,5};
Arrays.sort(num);
for (int i=0;i<num.length;i++)
{
System.out.println(num[i]);
}
}
}
自定义类产生对象排序
rihiroshitekiMacBook-Air:test LiBo$ java ArrayTest
number=10,name=lisi
number=20,name=zhangsan
number=30,name=libo
rihiroshitekiMacBook-Air:test LiBo$ cat ArrayTest.java
import java.util.Arrays;
class ArrayTest
{
public static void main(String[] args)
{
Student[] ss=new Student[] {new Student(20,"zhangsan"),
new Student(10,"lisi"),
new Student(30,"libo")};
Arrays.sort(ss);
for (int i=0;i<ss.length;i++)
{
System.out.println(ss[i]);
}
}
}
class Student implements Comparable
{
int num;
String name;
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public String toString()
{
return "number="+num+",name="+name;
}
public int compareTo(Object o)
{
Student s=(Student)o;
return num>s.num ? 1:(num==s.num ? 0 : -1);
}
}