当前位置:网站首页>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.
边栏推荐
- 防火墙基础之源NAT地址转换和服务器映射web页面配置
- 事件监听机制
- 华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- Ping An technology's practice of migrating from Oracle to ubisql
- Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
- d3dx9_ How to repair 38.dll? d3dx9_ 38. How to download a missing DLL?
- [multithreading] thread communication scheduling, waiting set wait(), notify()
- What do fast fashion brands care more about?
- 一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】
猜你喜欢

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

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

Kubernetes基础自学系列 | Ingress API讲解

List转Table

Source NAT address translation and server mapping web page configuration of firewall Foundation

阿里云刘珅孜:云游戏带来的启发——端上创新
#yyds干货盘点#简述chromeV8引擎垃圾回收

Annual comprehensive analysis of China's audio market in 2022
![[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth](/img/70/fa79ba38e28c41ed28bce2ec73cd79.png)
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth

Determine the maximum number of specific words in a string
随机推荐
QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
关于#mysql#的问题:问题遇到的现象和发生背景
Taishan Office Technology Lecture: the first difficulty is vertical positioning
Nemo of pulseaudio (22)
The array of C language is a parameter to pass a pointer
# Cesium实现卫星在轨绕行
Oracle concept II
数据中心表格报表实现定制统计加班请假汇总记录分享
How to improve it electronic equipment performance management
Autodesk NavisWorks 2022 software installation package download and installation tutorial
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
3.1 simple condition judgment
Popularization of MCU IO port: detailed explanation of push-pull output and open drain output
C language set operation
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
Under the influence of external factors, how to manage internal resources and reduce costs is the key
Li Chuang EDA learning notes 16: array copy and array distribution
QT5 之信号与槽机制(演示控件自带的信号与槽函数关联)
10分钟掌握mysql的安装步骤