数组

数组的快速排序:

import java.util.Arrays;
public class TestArr {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] a = new int[]{2,3,4,5,2,4,5};
		Arrays.sort(a);
		for (int i : a) {
			System.out.println(i+"\t");
		}
	}
}

数组的复制

1.  用for循环复制

public class Testcopy {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 使用循环语句进行赋值
		int [] a = new int []{1,2,3};
		int [] b = new int[a.length];
		for (int i = 0; i < a.length; i++) {
			b[i] = a[i];
		}
		System.out.println("数组b的值为:");
		for (int i = 0; i < b.length; i++) {
			System.out.println(b[i]);			
		}
	}
}
//用Arrays类的copyOf方法和copyOfRange()方法来实现数组的复制。
		int [] c = new int[]{4,5,3,5,6};
		System.out.println("数组c的值为:");
		for (int i : c) {
			System.out.println(i+"\t");
		}
		int [] d = Arrays.copyOf(c, 3);
		System.out.println("数组d的值为");
		for ( int j : d) {
			System.out.println(j+"\t");
		}
使用 copyOfRange() 方法实现数组的复制

Arrays.copyOfRange(数组名, 起始下标,结束下标)

一定要注意,下标是从0开始的

int [] a = {2,3,4,6,4,5};
		System.out.println("数组a的值:");
		for (int i : a) {
			System.out.print(i+"\t");
		}
		int[] b = Arrays.copyOfRange(a, 2, 4);
		System.out.println("\n"+"数组b的值为:");
		for (int j : b) {
			System.out.print(j+"\t");
		}

//使用arrayCopy()复制

        //arryCopy(原数组,原数组起始下标,目标数组,目标数组的起始下标,复制长度);

int [] a = new int[]{2,3,4,5,6,2,7};
		System.out.print("数组a的元素:");
		for (int i : a) {
			System.out.print(i+"\t");
		}
		int []b =new int[a.length];
		System.arraycopy(a, 2, b, 0, 5);
		System.out.print("\n"+"数组b的元素");
		for (int i : b) {
			System.out.print(i+"\t");
		}

数组的替换

public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*数组的替换
		 * fill(数组名,替换的值);
		 * 或fill(数组名,起始下标,结束下标,替换的值);
		 */
		int [] a = new int[]{1,2,3,4,5,6,7,8,9};
		System.out.print("数组a的元素:");
		for (int i : a) {
			System.out.print(i+"\t");
		}
		System.out.print("\n"+"第一次替换:");
		Arrays.fill(a, 5);
		for (int i : a) {
			System.out.print(i+"\t");
		}
		System.out.print("\n"+"第二次替换:");
		Arrays.fill(a, 2,5,0);
		for (int i : a) {
			System.out.print(i+"\t");
		}
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值