java基础知识(数组)

数组的定义

类型[]数组名;//常用 int[]a; double[] b;char[] c

类型数组名[];//int a[]


数组的初始化

静态初始化

初始化时由程序员显示的指定每个数组元素的初始值,由系统决定数组的长度。


int[]a;

a= new int[]{1,2,3,4,5};


int[]a = new int[]{1,2,3,4,5};

int[]a = {1,2,3,4,5};//简化写法


动态初始化

初始化时程序员只指定数组的长度,而不指定数组中元素的值,系统自动给数组分配初始值。


int[]a;

a= new int[5];


int[]a = new int[5];

初始值

byteshortintlong------------------> 0

floatdouble -------------------->0.0

char -------------------->‘\u0000’

boolean -------------------->false


引用类型:数组、类、接口--------------------> null

访问数组元素

数组中每个元素都有一个下标,下标从0开始:访问数组元素可以根据数组下标。

int[]a = {55,66,85,32,75};

55------------ a[0]

66------------ a[1]

85------------ a[2]

32------------ a[3]

75------------ a[4]


单独输出某个元素

System.out.println(a[2]);


输出所有元素(使用循环)

public class Test1{
	public static void main(String[] args){
		int[] a = {55,23,89,64,85};
		for(int i=0;i<=4;i++){
			System.out.println(a[i]);
		}
	}
}

//对前面循环输出数组元素改进

public class Test1{
	public static void main(String[] args){
		int[] a = {55,23,89,64,85};
		for(int i=0;i<=a.length-1;i++){
			System.out.println(a[i]);
		}
	}
}


//JDK1.5之后循环输出数组也可以只是用 foreachforeachJDK1.5开始引入)

public class Test1{
	public static void main(String[] args){
		int[] a = {55,23,89,64,85};
		//for循环输出
		for(int i=0;i<=a.length-1;i++){
			System.out.println(a[i]);
		}
		System.out.println("-------------------------------");
		//foreach输出
		for(int x:a){
			System.out.println(x);
		}
	}
}


数组的应用

求多个数据的和

public class Test1{
	public static void main(String[] args){
		int[] a = {55,23,89,64,85};
		int sum=0;
		for(int i=0;i<=a.length-1;i++){
			sum= sum+a[i];
		}
		System.out.println("总分="+sum);
	}
}


求多个数据中的最大值、最小值

public class Test1{
	public static void main(String[] args){
		int[] a = {55,23,89,64,85};
		int max = a[0];//假设第一个最大
		for(int i=0;i<=a.length-1;i++){
			if(a[i]>max){
				max= a[i];
			}
		}
		System.out.println("最大值="+max);
	}
}

public class Test1{
	public static void main(String[] args){
		int[] a = {55,23,89,64,85};
		int min = a[0];//假设第一个最小
		for(int i=0;i<=a.length-1;i++){
			if(a[i]<min){
				min= a[i];
			}
		}
		System.out.println("最小值="+min);
	}
}


查找是否有xx分的同学

import java.util.*;
public class Test1{
	public static void main(String[] args){
		Scanners = new Scanner(System.in);
		int x ;
		boolean f = false;//f表示是否存在要查找的值
		int[] a = {55,23,89,64,85};
		System.out.print("请输入你要查找的分数:");
		x= s.nextInt();
		for(int i=0;i<=a.length-1;i++){
			if(a[i]==x){
				f= true;
			}
		}
		if(f){
			System.out.println("有!");
		}else{
			System.out.println("无!");
		}
	}
}


对分数从高到低排序、从低到高排序(冒泡排序)

//从大到小

public class Test1{
	public static void main(String[] args){
		int[] a = {55,23,89,64,85};
		//从大到小冒泡排序
		for(int i = 0;i<a.length-1;i++){
			for(int j=0;j<a.length-1;j++){
				if(a[j]<a[j+1]){
					//交换
					int x = a[j];
					a[j]= a[j+1];
					a[j+1]= x;
				}
			}
		}
		//输出数组
		for(int i=0;i<=a.length-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
}



//从小到大

public class Test1{
	public static void main(String[] args){
		int[] a = {55,23,89,64,85};
		//从小到大冒泡排序
		for(int i = 0;i<a.length-1;i++){
			for(int j=0;j<a.length-1;j++){
				if(a[j]>a[j+1]){
					//交换
					int x = a[j];
					a[j]= a[j+1];
					a[j+1]= x;
				}
			}
		}
		//输出数组
		for(int i=0;i<=a.length-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
}



数组内存分析

:基本数据类型变量值,引用类型变量的地址值

:引用类型的真实值

案例一:

public class TestArray1{
	public static void main(String[] args){
		int[] a = {89,75,64,32,78};
		int[] b = a;
		System.out.println(a[3]);//32
		System.out.println(b[3]);//32
		a[1]= 100;
		System.out.println(a[1]);//100
		System.out.println(b[1]);//100
		b[3]= 50;
		System.out.println(a[3]);//50
		System.out.println(b[3]);//50
	}
}



案例二:

public class TestArray1{
	public static void main(String[] args){
		int[] a = {89,75,64,32,78};
		int[] b = {55,66,77};
		System.out.println(a[1]);//75
		System.out.println(b[1]);//66
		b= a;
		System.out.println(a[1]);//75
		System.out.println(b[1]);//75
		b[1]= 30;
		System.out.println(a[1]);//30
		System.out.println(b[1]);//30
	}
}



多维数组

Java中没有多维数组,所有的数组都可以看做一维数组

public class Test1{
	public static void main(String[] args){
		int a = 10;	//变量a
		int[] b = {10,20,30};	//一维数组b b中保存的是一个一个的变量
		int[][] c = {	//二维数组c c中保存的是一个一个的一维数组
			{10,20},	//c[0]
			{30,40},	//c[1]
			{40,50}	//c[2]
		};
		int[][][] d = {	//三维数组d d中保存的是一个一个的二维数组
			{{10,20},{30,40}},	//d[0]
			{{60,70},{80,90}}	//d[1]
		};
		System.out.println(a);//10
		System.out.println(b[0]);//10
		System.out.println(c[0][0]);//10
		System.out.println(c[2][1]);//50
		System.out.println(d[0][0][0]);//10
		System.out.println(d[1][0][0]);//60
		System.out.println(d[1][1][1]);//90
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值