当前位置:网站首页>Arrays API

Arrays API

2022-06-24 15:23:00 HLee

copyOf

Arrays Class copyOf(U[] original, int newLength, Class<? extends T[]> newType) Method to copy the specified array original To new array , The length of the new array is newLength, The new array element type is newType.

  1. If the new array is longer than the old array , That extra part is for null fill ;
  2. If the length of the new array is smaller than that of the old array , So few parts are directly intercepted ;
Long[] array1 = new Long[]{1L, 2L, 3L};

Object[] array2 = Arrays.copyOf(array1, 5, Object[].class);
System.out.println(Arrays.toString(array2)); // [1, 2, 3, null, null]

Object[] array3 = Arrays.copyOf(array1, 2, Object[].class);
System.out.println(Arrays.toString(array3)); // [1, 2]

asList

explain :Arrays.asList Will convert the array to ArrayList<>.

 Source code :
public static <T> List<T> asList(T... a) {
   return new ArrayList<>(a);
}
public class test {
  public static void main(String[] args) {
   
    String[] str = new String[5];
    str[0] = "a";
    str[1] = "b";
    str[2] = "c";
    str[3] = "d";
    str[4] = "e";

    // call Arrays Medium asList Methods will String[] Turn into a List<String>
    List<String> list = Arrays.asList(str);
    System.out.println("list:" + list.toString());

    // Again list Add elements individually 
    list.add("f");
    list.add("g");
    System.out.println("list:" + list.toString());
  }
}

 normal Arrays.asList(str) No problem , But in list.add("f")、list.add("g") It's time UnsupportedOperationException abnormal .

explain : Because in Arrays.asList When ,Arrays Class is return new ArrayList<>(a) An interior ArrayList class ( Member inner class ), This inner class inherits AbstractList class , Because the inner class has no new parent class add Method , Turn to add The... Of the parent class will be called during the operation add Method , and AbstractList stay add When they throw UnsupportedOperationException abnormal . But because of ArrayList Classes are also integrated AbstractList The parent class also overrides add、remove Operating the index does not call the methods of the parent class , So no exception will be thrown .

 Source code :AbstractList class 

public void add(int index, E element) {
   throw new UnsupportedOperationException();
}

public E remove(int index) {
   throw new UnsupportedOperationException();
}
 Mode one :
String[] strArray = new String[2];
ArrayList<String> list = new ArrayList<String>(Arrays.asList(strArray)) ;
list.add("1");
System.out.println(list);

 Mode two :( Most efficient )
String[] strArray = new String[2];
ArrayList< String> arrayList = new ArrayList<String>(strArray.length);
Collections.addAll(arrayList, strArray);
arrayList.add("1");
System.out.println(arrayList);

 Mode three :
List<Integer> intList= Arrays.stream(new int[] { 1, 2, 3, }).boxed().collect(Collectors.toList());
List<Long> longList= Arrays.stream(new long[] { 1, 2, 3 }).boxed().collect(Collectors.toList());
List<Double> doubleList= Arrays.stream(new double[] { 1, 2, 3 }).boxed().collect(Collectors.toList());

String[] arrays = {"tom", "jack", "kate"};
List<String> stringList= Stream.of(arrays).collect(Collectors.toList());
原网站

版权声明
本文为[HLee]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210512184140025v.html