数组初步

数组理解:
 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());
		}
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值