数组的补充

1、一个元素一个元素复制

public class mei01 {
	public static void main(String[] args) {
		int []a = new int[] {2,8,22,35,37};
		int []b = new int[a.length];
		System.out.println("\r使用逐一复制的方式:");
		for(int i=0;i<a.length;i++) {
			b[i]=a[i];
		}
		for(int m:b) {
			System.out.println(m+" ");
		}
	}
}

2、使用System.arrycopy()方法
格式:System.arraycopy(源数组名,源数组的起始下标,目标数组名,目标数组的起始下标, 复制的数组元素个数);

public class mei02 {
	public static void main(String[] args) {
		int []a = new int[] {2,8,22,35,37};
		int []b = new int[a.length];
		System.out.println("使用arraycopy方法:");
		System.arraycopy(a,0,b,0,a.length);
		for(int m:b) {
			System.out.println(m+" ");
		}
System.out.println("***********************************");
		int []c = new int[] {2,8,22,35,37};
		int []d = new int[c.length];
		System.out.println("使用arraycopy方法:");
		System.arraycopy(c,2,d,0,c.length-2);
		for(int m:d) {
			System.out.println(m+" ");
		}
	}
}

3、使用Arrays类中的copyOf()或copyOfRange()方法
格式:目标数组=Arrays.copyOf(源数组,复制的数组元素个数)格式:目标数组=Arrays.copyOfRange(源数组, 源数组的起始下标, 源数组的终止下标);
注意事项:复制的元素不包括源数组的终止下标代表的元素
数组元素复制的总结:
①使用Arrays类中的copyOf()或copyOfRange()方法,目标数组将被重新定义数组大小;
②b=a;//将a的引用赋值给b,并没有实现复制。

import java.util.Arrays;

public class mei03 {
	public static void main(String[] args) {
		int []a = new int[] {2,8,22,35,37};
		int []b = new int[a.length];
		b = Arrays.copyOf(a, 5);
		for(int m:b) {
			System.out.println(m+" ");
		}
System.out.println("*********************************************");
		int []c = new int[] {2,8,22,35,37};//
		int []d = new int[c.length];
		b = Arrays.copyOfRange(c, 2, 4);
		for(int m:d) {
			System.out.println(m+" ");
		}
	}
}

4、矩阵乘法

public class mei04 {
	public static void main(String[] args) {
		int [][]a ={{1,2},{3,4}};
		int [][]b ={{2,1,3},{3,2,1}};
		int [][]c =new int[2][3];
		for (int i=0;i<c.length;i++) {//A的行
			for (int j= 0;j<c[0].length;j++) { //B的列
				for (int k = 0;k<2;k++) //A的列和B的行一致
					c[i][j] =c[i][j]+a[i][k]*b[k][j];
				}
			}
		for (int i=0;i<c.length;i++) {
			for (int j= 0;j<c[0].length;j++)
				System.out.print(c[i][j]+" ");
				System.out.println();
			}
	}
}

5、不规则的二维数组

public class mei05 {
	public static void main(String[] args) {
//		指定第一维的大小,第二维的大小不指定
		String[][]students = new String [2][];
//		students[0]有3个元素
		students[0]=new String[3];
//		students[1]有2个元素		
		students[1]=new String[2];
		students[0][0]="小明";
		students[0][1]="小强";
		students[0][2]="小黑";
		students[1][0]="小红";
	students[1][1]="小美";
	}
}

6、杨辉三角形

public class mei06 {
	public static void main(String[] args) {
		int i ,j;
		int [][]hui =new int [10][];
		for(i=0;i<hui.length;i++)
		hui[i]= new int[i+1];
		hui[0][0]=1;
		for(i=1;i<hui.length;i++) {							
			hui[i][0]=1;
			for(j=1;j<hui[i].length-1;j++)
			hui[i][j]=hui[i-1][j-1]+hui[i-1][j];
			hui[i][hui[i].length-1]=1;
		}
		for(i=0;i<hui.length;i++) {
			for(j=0;j<hui[i].length;j++)
			System.out.print(hui[i][j]+" ");
			System.out.println();
		}
	}
}

一、Arrays类
Arrays类,定义了若干静态方法对数组的操作
(一)数组的排序sort()
注意事项:
1、使用步骤:导包,创建,使用
2、可以对基本类型和引用类型数组进行排序
3、默认的排序方式是升序,对象数组根据Unicode码进行升序
(二)在已排序数组中查找元素binarySearch()
格式:Arrays.binarySearch(已排序的数组, 需要查找的数);
(1)使用binarySearch(),必须在已排序的数组中进行
(2)能查找到的情况下,返回值为该数的下标
(3)不能查找到的情况下,返回值为该数插入数组的"-(插入点-1)"

import java.util.Arrays;

public class mei07 {
	public static void main(String[] args) {
		int []a = {23,12,45,32,5};
		Arrays.sort(a);
		for(int i:a) 
		{
		System.out.print(i+" ");
		}System.out.println();
		//对象数组类型必须实现comparable接口
		String []names = {"china","usa","france","england"};
		for (String j : names) {
		System.out.print(j+" ");
		}
		System.out.println();
		Arrays.sort(names);
		for (String j : names) {
			System.out.print(j+" ");
		}
		System.out.println();
	}
}

(二)在已排序数组中查找元素binarySearch()
格式:Arrays.binarySearch(已排序的数组, 需要查找的数);
(1)使用binarySearch(),必须在已排序的数组中进行
(2)能查找到的情况下,返回值为该数的下标
(3)不能查找到的情况下,返回值为该数插入数组的"-(插入点-1)"

import java.util.Arrays;

public class mei07 {
		int i=Arrays.binarySearch(a, 4);
		System.out.println(i);//-1,4没有在数组中
		int j=Arrays.binarySearch(a, 45);
		System.out.println(j);//4,45在数组中且下标为4
}

(三)数组元素的复制copyOf()或copyOfRange()
格式:Arrays.copyOf(源数组名, 需要复制的元素个数);
(1)需要复制的个数<=>源数组
(2)<=源数组个数:复制部分或者全部(可用源数组名.length表示全部)
(3)>源数组个数:目标长度数组变更,复制源数组全部元素,其余用默认值占位
格式:Arrays.copyOfRange(源数组名, 起始元素下标, 目标元素下标+1);

import java.util.Arrays;

public class mei08 {
	public static void main(String[] args) {
		int []a = {23,12,45,32,5};
		int []b=Arrays.copyOf(a, 4);
			for(int i:b) 
			{
			System.out.print(i+" ");//23 12 45 32
		}System.out.println();	
		
		int []c =Arrays.copyOfRange(a, 2, 3);
		for (int i : c) {
			System.out.print(i+" ");
		}System.out.println();
	}
}

(四)填充数组fill()
(1)全部填充的格式:Arrays.fill(目标数组名,需要填充的值)
(2)部分填充的格式:Arrays.fill(目标数组,起始元素下标,结束元素下标+1,需要填充的数据)
Arrays.fill(a,2,5,23);//在a中,从下标为2的元素开始填充,填充至下标为4的元素,分别填充23

import java.util.Arrays;

public class mei09 {
public static void main(String[] args) {
int []a= new int [10];
	for(int i:a) 
	{
	System.out.print(i+" ");
	}System.out.println();	
	Arrays.fill(a, 23);
	for(int i:a) 
	{
		System.out.print(i+" ");
	}System.out.println();
System.out.println("*********************************************");
	int []a= new int [10];
Arrays.fill(a,2,5,23);//在a中,从下标为2的元素开始填充,填充至下标为4的元素,分别填充23
	}
}

(五)数组的比较equals()
格式:Arrays.equals(a, b);
注意事项:
1、结果为boolean值,只要有一个数不一样,输出就是false
2、对象数组同样可以用equals()方法进行比较

import java.util.Arrays;

public class mei10 {
	public static void main(String[] args) {
		int []a= {12,23,56,34,1};
		int []b= {12,23,56,34,1};
		boolean num =Arrays.equals(a, b);
		System.out.println(num);
System.out.println("*********************************************");		
		int []c= {12,23,56,34,1};
		int []d= {12,23,56,34,1};
		boolean num1=Arrays.equals(c, d);
		System.out.println(num1);
System.out.println("*********************************************");
		String []e= new String [5];
		Arrays.fill(e, "hi");
		String []f= {"hi","hi","hi","hi","hi"};
	System.out.println(Arrays.equals(e, f));
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值