数组理解:
int[] arr=new int[10];
1、定义好的数组不能改变长度,只能改变每个元素的值
2、基本数组类型:常用的有int boolen char
3、基本数组:定义以后会有默认值0
引用类型数组:定义后无默认值{输出null,只是分配了n个字符串大小的内存空间}
数组自己本身也是一种引用类型
String[] strArr=new String[10]
4、引用类型数组定以后一定要记得赋值 strArr[i]="str"+i;
在定义时赋值:int[] arr2={1,2,3} 或者 arr2=new int[]{1,2,3};
String[] strArr2=new String{"qa","dh“}或者
String[] strArr3={"qa","dh“}
5、怎样将数组里的数据清除掉?arr2=new int[3];或者arr2=new int[art2.length];
6、多维数组:只要第一维需要指定长度。第二维长度也可以设定
int[][] arr3=new int[10][];
arr3[0]=new int[]{1,1,1,1}
arr3[1]=new int[]{2,2}也是可以的
定义时赋值:括号欠括号
int[][][] arr4=new int{{},{}}
import java.lang.reflect.Array;
import java.util.Random;
public class Student {
//设置Student属性
private String name;
private int age;
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public static void main(String[] args){
Student stt=new Student();
stt.sort();
}
public void sort(){
Random r=new Random();
Student[] stu1= new Student[10];
System.out.println("排序前: ");
for(int i=0;i<stu1.length;i++){
Student st = new Student();
st.name = "name"+i;
st.age = r.nextInt(30);
stu1[i]=st;
System.out.println(stu1[i].toString());
}
System.out.println("排序后: ");
for(int j=0;j<stu1.length;j++){
for(int k=j+1;k<stu1.length;k++){
if(stu1[j].age>stu1[k].age){
Student tem=stu1[j];
stu1[j]=stu1[k];
stu1[k]=tem;
}
}
System.out.println(stu1[j].toString());
}
}
}