日期:2014-05-20  浏览次数:20826 次

数组的相关操作--------孙鑫视频
import   java.util.Arrays;

class     ArrayTest
{
public   static   void   main(String[]   args)  
{
int[]   num1   =   new   int[]{1,2,3};
int[]   num2   =   new   int[3];
int[]   num3   =   new   int[10];

System.arraycopy(num1,0,num2,0,num1.length);
for   (int   i   =   0;   i   <   num2.length;   i++)
{
System.out.println(num2[i]);
}

System.arraycopy(num1,1,num3,8,2);
for   (int   i   =   0;   i   <   num3.length;   i++)
{
System.out.println(num3[i]);
}

Point[]   pts1   =   new   Point[]{new   Point(1,1),new   Point(2,2),new   Point(3,3)};
Point[]   pts2   =   new   Point[3];
System.arraycopy(pts1,0,pts2,0,pts1.length);
for   (int   i   =   0;   i   <   pts2.length;   i++)
{
System.out.println( "x= "+pts2[i].x+ ",y= "+pts2[i].y);
}

pts2[1].x   =   5;
pts2[1].y   =   5;
System.out.println( "x= "+pts1[1].x+ ",y= "+pts1[1].y);

int[]   num4   =   new   int[]{3,1,2};
Arrays.sort(num4);
for   (int   i   =   0;   i   <   num4.length;   i++)
{
System.out.println(num4[i]);
}

int   index   =   Arrays.binarySearch(num4,3);
System.out.println( "index= "+index);
System.out.println( "element= "+num4[index]);
Student[]   ss   =   new   Student[]{new   Student( "zhangsan ",3),new   Student( "lisi ",2),new   Student( "wangwu ",1)};
Arrays.sort(ss);

for   (int   i   =   0;   i   <   ss.length;   i++)
{
System.out.println(ss[i]);
}
}
}

class   Point
{
int   x,y;
Point(int   x,int   y)
{
this.x   =   x;
this.y   =   y;
}
}

class   Student   implements   Comparable
{
int   num;
String   name;

Student(String   name,int   num)
{
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);

}

}

return   num   >   s.num   ?   1   :   (num   ==   s.num   ?   0   :   -1);这个不理解
不是三元表达式不理解
而是   num   >   s.num   ?   1   这里的num是什么?
是传过来的num吗?那么他和s.num不是恒相等的吗?

------解决方案--------------------
num是当前对象里的,若是这样写你就更明白了:this.num。而你看Student s = (Student)o,说明s是通过参数传过来的另一个对象了。