当前位置:网站首页>Ten common methods of arrays tools
Ten common methods of arrays tools
2022-06-27 16:57:00 【Iron helmet and anchor】
interpreter : Anchor
Translation date : 2013 year 11 month 19 Japan
Link to the original text : Top 10 Methods for Java Arrays
The following is by stackoverflow Website Voted Java The ten most commonly used methods for arrays ,
0. Declaration array
String[] aArray = new String[5];
String[] bArray = {"a","b","c", "d", "e"};
String[] cArray = new String[]{"a","b","c","d","e"};
- 1.
- 2.
- 3.
1. Print array
int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);
// Print directly , Will print the reference object Hash value
// [[email protected]
System.out.println(intArray);
// [1, 2, 3, 4, 5]
System.out.println(intArrayString);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
2. Create... From an array ArrayList
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
// [a, b, c, d, e]
System.out.println(arrayList);
- 1.
- 2.
- 3.
- 4.
3. Check whether the array contains a value
String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
// true
System.out.println(b);
- 1.
- 2.
- 3.
- 4.
4. Merge and join two arrays
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
- 1.
- 2.
- 3.
- 4.
5. Declare inline arrays
method(new String[]{"a", "b", "c", "d", "e"});
- 1.
6. Link with the given string (join) Array
// containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
// a, b, c
System.out.println(j);
- 1.
- 2.
- 3.
- 4.
- 5.
7. take ArrayList Convert to array
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
8. Convert an array to Set
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
//[d, e, b, c, a]
System.out.println(set);
- 1.
- 2.
- 3.
9. Array element inversion
int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
//[5, 4, 3, 2, 1]
System.out.println(Arrays.toString(intArray));
- 1.
- 2.
- 3.
- 4.
10. Remove elements
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);// Create a new array
System.out.println(Arrays.toString(removed));
- 1.
- 2.
- 3.
more —— transformation int Values are byte arrays
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
System.out.format("0x%x ", t);
}
- 1.
- 2.
- 3.
- 4.
- 5.
边栏推荐
- Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
- Oracle concept II
- When the publish / subscribe mode encounters NET
- 事件监听机制
- About MySQL: the phenomenon and background of the problem
- Unity 阴影——阴影平坠(Shadow pancaking)
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- Adaoracle supports multi chain distributed Oracle with wide area node quotation
- Open source 23 things shardingsphere and database mesh have to say
- 当发布/订阅模式遇上.NET
猜你喜欢

Event listening mechanism

Simulated process scheduling

Oracle概念三

Li Chuang EDA learning notes 16: array copy and array distribution

Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end

Kubernetes basic self-study series | introduction to ingress API

Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance

Annual comprehensive analysis of China's audio market in 2022

Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)

关于#mysql#的问题:问题遇到的现象和发生背景
随机推荐
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
Adaoracle supports multi chain distributed Oracle with wide area node quotation
事务的隔离级别详解
What should the ultimate LAN transmission experience be like
QT5 之信号与槽机制(演示控件自带的信号与槽函数关联)
Leetcode daily practice (longest substring without repeated characters)
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
【多线程】线程通信调度、等待集 wait() 、notify()
What is RPC
C language course design
Unity shadow shadow pancaking
Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines
Open source 23 things shardingsphere and database mesh have to say
Logstash excludes specific files or folders from collecting report log data
The array of C language is a parameter to pass a pointer
数据中心表格报表实现定制统计加班请假汇总记录分享
The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle
3.1 simple condition judgment
模拟进程调度