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));
}
}
java11:数组试用
原文作者:浪漫的偷笑
原文地址: https://blog.csdn.net/u013304677/article/details/83043756
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/u013304677/article/details/83043756
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。