java11:数组试用

package day11;

import java.util.Arrays;

public class Demo01 {

	public static void main(String[] args) {
		String[] names = {"张三","李四","王五","赵六","孙八"};
		
		//数组转字符串
		System.out.println(Arrays.toString(names));
		
		for(int i = 0;i<names.length;i++){
			System.out.print(names[i]);
		}
		
		
		System.out.println('\n');
		
		//数组比较
		String[] answer ={"张三","李四"};
		String[] user1 ={"李四","张三"};
		String[] user2 ={"王五","赵六"};
		String[] user3 ={"张三","李四"};
		System.out.println(Arrays.equals(answer, user1));
		System.out.println(Arrays.equals(answer, user2));
		System.out.println(Arrays.equals(answer, user3));
		
		//排序
		Arrays.sort(names);//从小到大 按照编码值
		System.out.println(Arrays.toString(names));
		
		
		//二分查找 要先排序 再查找  返回 元素的位置,返回 负数 就是没有找到
		
		String[] math = {"2","6","3","1","2","7","4","3","9"};
		System.out.println(Arrays.binarySearch(math, "9"));		
		System.out.println(Arrays.binarySearch(math, "1"));
		System.out.println(Arrays.binarySearch(math, "2"));
		Arrays.sort(math);
		System.out.println(Arrays.binarySearch(math, "1"));
		System.out.println(Arrays.binarySearch(math, "2"));
		System.out.println(Arrays.binarySearch(math, "9"));
		System.out.println(Arrays.binarySearch(math, "5"));
		
		
		//复制数组
		int[] array = {1,2,3};
		int[] array2 = array;
		System.out.println(array);
		System.out.println(array2);
		
		
		String[] math2 = new String[math.length];
		for (int i=0;i<math.length;i++){
			math2[i] =math[i];
		}
		math2[3]= "200";
		System.out.println(Arrays.toString(math));
		System.out.println(Arrays.toString(math2));
		
		//api  复制 速度比 for 快的快的快
		String[] cpmath = new String[math.length];
		System.arraycopy(math, 0, cpmath, 0, math.length);
		System.out.println(Arrays.toString(cpmath));
		
		//java1.5以上 复制
		String[] cpmath2 =Arrays.copyOf(math, math.length);
		System.out.println(Arrays.toString(cpmath2));
		
		
		//数组扩容 
			//数组长度是不可改变的
			//数组变量可以重新复制引用不通的数组
			//利用更换数组可以实现数组的扩容
		
		int[] array3 = {1,2,3};
		System.out.println(array3);
		System.out.println(Arrays.toString(array3));
		array3 =new int[]{1,2,3,4,5,6};
		System.out.println(array3);
		System.out.println(Arrays.toString(array3));
		
		
		//扩容算法,缩减
		array3 = Arrays.copyOf(array3,array3.length+1 );
		System.out.println(Arrays.toString(array3));
		array3 = Arrays.copyOf(array3,array3.length-3 );
		System.out.println(Arrays.toString(array3));
		//追加元素 
		array3[array3.length-1] = 7;
		System.out.println(Arrays.toString(array3));
		

		
		

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值