当前位置:网站首页>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.
- If the new array is longer than the old array , That extra part is for null fill ;
- 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());边栏推荐
- SF express: please sign for MySQL soul ten
- As a developer, what is the most influential book for you?
- Py's toad: a detailed introduction to toad, its installation and use
- "Industry outlook" analysis of five major trends in China's security video surveillance industry
- Method after charging the idea plug-in material theme UI
- Left hand code, right hand open source, part of the open source road
- 缓存使用中Redis,Memcached的共性和差异分析
- 在同花顺开户证券安全吗,需要什么准备
- Carry forward the fine style of continuous operation and go all out to ensure the safety of Beijiang Levee
- ES mapping之keyword;term查詢添加keyword查詢;更改mapping keyword類型
猜你喜欢

Port conflict handling method for tongweb
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

从pair到unordered_map,理论+leetcode题目实战

Keyword of ES mapping; Term query add keyword query; Change mapping keyword type

Laravel 8 realizes auth login

Linux Installation cenos7 MySQL - 8.0.26

API data interface for announcement of Hong Kong listed companies

同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩

Wide measuring range of jishili electrometer
Redis consistency hash and hash slot
随机推荐
The future of robots -- deep space exploration
openinstall携手书链:助力渠道数据分析,共创书联网时代
This website teaches you to imitate more than 100 well-known websites!
Data sharing between laravel lower views
Keyword of ES mapping; Term query add keyword query; Change mapping keyword type
Task priority motion planning of floating base
Left hand code, right hand open source, part of the open source road
Typescript raw data type
Attacked! Cloud development monitoring alarm practice
Actual combat | a tortuous fishing counteraction
Istio Troubleshooting: using istio to reserve ports causes pod startup failure
阿里OSS对象存储服务
Huangchuping presided over the video conference on fixed-point contact with Zhuhai, resolutely implemented the deployment requirements of the provincial Party committee, and ensured positive results i
中国十大证券app排名 炒股开户安全吗
Low fidelity prototype vs high fidelity prototype, which one is more suitable for your design?
Closed loop management of time synchronization service -- time monitoring
[log service CLS] a taste of Tencent cloud log service CLS
FPGA based analog I ² C protocol system design (medium)
Data stack technology sharing: how to use data stack for data collection?
The industrial control security of roaming the Intranet