java入门数组基础
java数组的相关操作1.在Java 中所有的数组都在一个缺省的属性Length,用于获取数组中元素的个数.
2.数组的复制System.arraycopy()
3.数组的排序Arrays.sort()
4.在已排序的数组中查找某个元素Arays.binarySearch()*/
import java.util.Arrays; //对数组排序时要导入此包.
class ArrayTest
{
public static void main(String[] args)
{
/*int [] num1=new int [] {1,2,3};
int [] num2=new int ;
System.arraycopy(num1,0,num2,0,num1.length);
for(int i=0;i<num2.length;i++)
{
System.out.println(num2[i]);
}*/
//对象数组的copy
/*Point [] pts1=new Point []{ new Point(1,1),new Point(2,2),new Point(3,3)};
Point [] pts2=new Point;
System.arraycopy(pts1,0,pts2,0,pts1.length);
for(int i=0;i<pts1.length;i++)
{
System.out.println("x="+pts1[i].x+","+"y="+pts1[i].y);
}
pts2.x=6;//对pts1.x也进行了修改.
pts2.y=6;//对pts1.y也进行了修改.
System.out.println("x="+pts1.x+","+"y="+pts1.y);*/
//数组的排序
/*int [] num=new int []{3,1,2};
Arrays.sort(num);
for (int i=0;i<num.length;i++)
{
System.out.println(num[i]);
}
int index=Arrays.binarySearch(num,3);
System.out.println("index="+index);
System.out.println("element="+num[index]);*/
Student [] st=new Student[]{new Student(1,"zhangsan"),
new Student(2,"lisi"),
new Student(3,"wangwu"),
new Student(3,"mybole")};
Arrays.sort(st);
for(int i=0;i<st.length;i++)
{
System.out.println(st[i]);
}
}
}
//要对对象进行排序必须实现comparable接口
class Student implements Comparable
{
int num;
String name;
Student(int num,String name)
{
this.num=num;
this.name=name;
}
//重写toString方法
public String toString()
{
return "number="+num+","+"name="+name;
}
public int compareTo(Object o)//覆盖Comparable中的CompareTo方法.
{
Student s=(Student)o;
//return num>s.num?1:(num==s.num?0:-1);
int rusult=num>s.num?1:(num==s.num?0:-1); //在num相同时,对name进行排序.
if(0==rusult)
{
rusult=name.compareTo(s.name);//对于比较字符类型的,在String类中已经重写的compareTo方法,可以直接使用.
}
return rusult;
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
} (随机推荐阅读本站500篇优秀文章点击前往:500篇优秀随机文章)
2.数组的复制System.arraycopy()
3.数组的排序Arrays.sort()
4.在已排序的数组中查找某个元素Arays.binarySearch()*/
import java.util.Arrays; //对数组排序时要导入此包.
class ArrayTest
{
public static void main(String[] args)
{
/*int [] num1=new int [] {1,2,3};
int [] num2=new int ;
System.arraycopy(num1,0,num2,0,num1.length);
for(int i=0;i<num2.length;i++)
{
System.out.println(num2[i]);
}*/
//对象数组的copy
/*Point [] pts1=new Point []{ new Point(1,1),new Point(2,2),new Point(3,3)};
Point [] pts2=new Point;
System.arraycopy(pts1,0,pts2,0,pts1.length);
for(int i=0;i<pts1.length;i++)
{
System.out.println("x="+pts1[i].x+","+"y="+pts1[i].y);
}
pts2.x=6;//对pts1.x也进行了修改.
pts2.y=6;//对pts1.y也进行了修改.
System.out.println("x="+pts1.x+","+"y="+pts1.y);*/
//数组的排序
/*int [] num=new int []{3,1,2};
Arrays.sort(num);
for (int i=0;i<num.length;i++)
{
System.out.println(num[i]);
}
int index=Arrays.binarySearch(num,3);
System.out.println("index="+index);
System.out.println("element="+num[index]);*/
Student [] st=new Student[]{new Student(1,"zhangsan"),
new Student(2,"lisi"),
new Student(3,"wangwu"),
new Student(3,"mybole")};
Arrays.sort(st);
for(int i=0;i<st.length;i++)
{
System.out.println(st[i]);
}
}
}
//要对对象进行排序必须实现comparable接口
class Student implements Comparable
{
int num;
String name;
Student(int num,String name)
{
this.num=num;
this.name=name;
}
//重写toString方法
public String toString()
{
return "number="+num+","+"name="+name;
}
public int compareTo(Object o)//覆盖Comparable中的CompareTo方法.
{
Student s=(Student)o;
//return num>s.num?1:(num==s.num?0:-1);
int rusult=num>s.num?1:(num==s.num?0:-1); //在num相同时,对name进行排序.
if(0==rusult)
{
rusult=name.compareTo(s.name);//对于比较字符类型的,在String类中已经重写的compareTo方法,可以直接使用.
}
return rusult;
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
} (随机推荐阅读本站500篇优秀文章点击前往:500篇优秀随机文章)
来源:本文由易搜IT博客原创撰写,欢迎分享本文,转载请保留出处和链接!