【java】数组(包含冒泡排序)

数组是一组同类型的数据或对象的集合。每个数组有一个名称,称为数组名,数组中的每个数据,称为数组元素

一维数组

数组的声明及其初始化

int a[]=new int[5];  //声明时必须声明数组容量

上面这种方式初始化时,数组的元素会有默认值,数字类型的默认值是0,对象的默认类型是null
在这里插入图片描述

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

数组的一个重要属性——length

length” 用于获取数组中元素的长度(元素的个数)。
数组一旦初始化,其长度是不可变的。
例如1:

int a[]=new int[10];
for(int i=0;i<a.length;++i){     //注意:a.length=10
	a[i]=i*i;
}

例如2:

int b[][]=new int[6][5];
for(int i=0;i<b.length;++i){  //注意:b.length=6
 	for(int j=0;j< b[i].length;++j){
		//注意:b[i].length=5
		a[i][j]=i*j;
	}
}

利用数组求1~100的和

public class test {
public static void main(String[] args) {
	int[] ia = new int[101];              //创建数组ia,数组的下标从0到100,共101个元素
    for (int i = 0; i < ia.length; i++) {       //利用循环语句,给数组元素一次赋值
    	ia[i] = i;
    }
    int sum = 0;
    for (int i = 0; i < ia.length; i++) {      //利用循环,累加求出所有元素的和值
 	   sum += ia[i];
    }
    System.out.println(sum);   //输出和值
  }
}

在这里插入图片描述

二维数组

二维数组的创建

二维数组的创建与一维数组类似,不同的是,需要指定两个数值(行数的值,列数值),
二维数组的创建有两种方式:
(1)方法一:直接分配空间(new),
例如:

int a[][];
a= new int[2][3]; //该数组2行3列,共5个元素

(2)方法二: 从最高维开始,为每一维分配空间,
例如:

int c[][];
c = new int[2][];   //定义2行
c[0] = new int[4];  //定义第一行有4个元素
c[1] = new int[3];   //定义第二行有3个元素

该数组为2行,共有7个元素,其中第一行4个元素,第2行3个元素。

初始化二维数组

初始化二维数组与一维数组类似,不同的是按行给出每行的元素值。
例如:

int a[][] = {{1,2,3}, {3,4,5}};  //数组是2行3列

该语句所形的数组为:
a[0][0]=1 a[0][1]=2 a[0][2]=3
a[1][0]=3 a[1][1]=4 a[1][2]=5
再例如:

 int b[][] = {{10,20,30}, {1,2,3,4,5}};
 //数组是2行,第一行3列,第2行5列。

在这里插入图片描述
求数组里数组元素的加和

public class test {
public static void main(String[] args) {
	int[][] a = new int[][] {
		{0,1,2},{3,4,5,6},{7,8,9,10}
	};
	int res = 0;
	for(int i = 0; i < a.length; i++) {
		for(int j = 0; j < a[i].length; j++) {
			res +=a[i][j];
		}
	}
	System.out.println(res);
  }
}

在这里插入图片描述
分析程序,给出运行结果。注意:该程序,采用对每个元素单独进行赋值,未赋值的采用默认值。

public class test {
public static void main(String[] args) {
	int a[][] = new int[3][3];
	 a[0][0] = 1;
	 a[1][1] = 2;
	 a[2][2] = 3;
	 System.out.println("数组a: ");
	 for (int i = 0; i < a.length; i++) {               //i控制行数
	       for (int j = 0; j < a[i].length; j++) {     //j控制列数
	               System.out.print(a[i][j] + " ");
	      }
	     System.out.println();
	}
  }
}

在这里插入图片描述

字符串数组

String[] strs = new String[5];
String[] strs = new String[]{"a","b","c","d","e"};

在这里插入图片描述
例:分析程序,理解字符串数组,并给出运行结果

public class test {
public static void main(String[] args) {
	//利用字符串常量创建并初始化数组
    String[] anArray1 = { "String One", "String Two", "String Three"};
    //以下分别声明、创建、赋值字符串数组
    String[] anArray2;              //声明字符串数组           
    anArray2=new String[3];     //创建字符串数组
    anArray2[0]="xxxxxxx";     //赋值字符串数组
    anArray2[1]="yyyyyyy";
    anArray2[2]="aaaaaaa";
    System.out.println("第一个字符串数组中的3个串值:");
    for (int i = 0; i < anArray1.length; i++){ 
        System.out.println( anArray1[i]);
    } 
     System.out.println("第二个字符串数组中的3个串值:");
    for (int i = 0; i < anArray2.length; i++){ 
        System.out.println( anArray2[i]);
    } 
  }
}

在这里插入图片描述

注意

int [] x,y[];   //x是一维数组,y是二维数组

数组中涉及的常见算法

1、求最大值

public class test {
public static void main(String[] args) {
	int[] a = new int [] {4,2,7,1,3,5};
	int max = a[0];  //假设a[0]是最大值
	for(int i = 0; i < a.length ; i++) {
		if(max < a[i]) {
			max = a[i]; //存放目前最大值
		}
	}
	System.out.println("max: " + max);
  }
}

在这里插入图片描述
2、求最小值 (同上)

public class test {
public static void main(String[] args) {
	int[] a = new int [] {4,2,7,1,3,5};
	int min = a[0];  //假设a[0]是最小值
	for(int i = 0; i < a.length ; i++) {
		if(min > a[i]) {
			min = a[i]; //存放目前最小值
		}
	}
	System.out.println("min: " + min);
  }
}

在这里插入图片描述
3、总和、平均数

public class test {
public static void main(String[] args) {
	int[] a = new int [] {4,2,7,1,3,5};
	double res = 0;
	for(int i = 0; i < a.length ; i++) {
		res += a[i];
	}
	System.out.println("总和: " + res);
	System.out.println("平均数:" + (res / a.length));
  }
}

在这里插入图片描述

4、 数组的复制

public class test {
public static void main(String[] args) {
	int[] a = new int [] {4,2,7,1,3,5};
	//声明一个与a长度一致的数组
	int[] copy = new int[a.length];
	for(int i = 0; i < a.length ; i++) {
		copy[i] = a[i];
	}
	for(int i = 0; i < a.length ; i++) {
		System.out.print(copy[i]+" ");
	}
  }
}

在这里插入图片描述
5、数组的反转

public class test {
public static void main(String[] args) {
	int[] a = new int [] {4,2,7,1,3,5};
	//声明一个与a长度一致的数组
	int[] temp = new int[a.length];
	int k = 0;//temp元素的下标
	for(int i = a.length - 1; i >= 0; i--) {
		temp[k] = a[i];
		k++;
	}
	for(int i = 0; i < a.length ; i++) {
		System.out.print(temp[i]+" ");
	}
  }
}

在这里插入图片描述
6、数组排序

冒泡排序
冒泡排序动态图
例如4,7,3,1进行冒泡排序(从小到大)
第一次排序结果:4,3,1,7(得到一个最大的数字,放在倒数第一位)
第二次排序结果:3,1,4,7(得到除最后一个数字外的最大数字,放在倒数第二位)
第三次排序结果:1,3,4,7

public class test {
public static void main(String[] args) {
	int[] a = new int [] {4,7,3,1};
	for(int i = 0; i < a.length - 1 ; i++) {
		//外层循环是循环轮次,轮次循环的次数是数组长度-1
		for(int j = 0; j < a.length-1-i; j++) {
			if(a[j] > a[j+1])
            {
                int temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
		}
	}
	System.out.println("从小到大排序后的结果是:");
    for(int i = 0; i < a.length; i++)
    {
        System.out.print(a[i]+" ");
    }
  }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值