【翻译】Java Array的排名前十方法(Top 10 Methods for Java Arrays)
这里列举了Java Array 的前十的方法。他们在stackoverflow最大投票的问题。
The following are top 10 methods for Java Array. They are the most voted questions from stackoverflow.
0.声明一个数组
0. Declare an array
String[] aArray = new String[5];
String[] bArray = {"a", "b", "c", "d", "e"};
String[] cArray = {"a", "b", "c", "d", "e"};
1.打印数组
1. Print an array in Java
int[] intArray = {1, 2, 3, 4, 5};
String intArrayString = Arrays.toString(intArray);
//直接输出Array,输出,内存地址:[I@4554617c
System.out.println(intArray);
//输出:[1, 2, 3, 4, 5]
System.out.println(intArrayString);
2.从数组中转为ArrayList
2. Create an ArrayList from an array
String[] stringArray = {"a", "b", "c", "d", "e"};
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(stringArray));
//输出[a, b, c, d, e]
System.out.println(arrayList);
3.判断数组是否含有某值
3. Check if an array contains a certain value
String[] stringArray = {"a", "b", "c", "d", "e"};
boolean b = Arrays.asList(stringArray).contains("a");
//true
System.out.println(b);
4.连接两个数组
4. Concatenate two arrays
//需要导入 org.apache.commons.lang3
int[] intArray1 = {1, 2, 3, 4, 5};
int[] intArray2 = {6, 7 , 8, 9, 10};
int[] combinedIntArray = org.apache.commons.lang3.ArrayUtils.addAll(intArray1, intArray2);
String arrayString = Arrays.toString(combinedIntArray);
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
System.out.println(arrayString);
5.Declare an array inline --- 不知道这个的作用
method(new String[]{"a", "b", "c", "d", "e"});
6.将数组的每个元素取出拼接成字符串
6. Joins the elements of the provided array into a single String
String j = org.apache.commons.lang3.StringUtils.join(new String[] { "a", "b", "c" }, ":");
//a:b:c
System.out.println(j);
7.将ArrayList转换为数组
7.Covnert an ArrayList to an array
String[] stringsArray = {"a", "b", "c", "d", "e"};
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(stringsArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
8.将数组转换为set
8. Convert an array to a set
String[] stringsArray = {"a", "b", "c", "d", "e"};
Set<String> set = new HashSet<String>(Arrays.asList(stringsArray));
System.out.println(set);
9.反转一个数组
9. Reverse an array
String[] stringsArray = {"a", "b", "c", "d", "e"};
ArrayUtils.reverse(stringsArray);
//[e, d, c, b, a]
System.out.println(Arrays.toString(stringsArray));
10.移除数组的某个元素
10. Remove element of an array
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));
One more - convert int to byte array
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
System.out.format("0x%x ", t);
}
【翻译】Java Array的排名前十方法(Top 10 Methods for Java Arrays)的更多相关文章
- Top 10 Methods for Java Arrays
作者:X Wang 出处:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/ 转载文章,转载请注明作者和出处 The ...
- Appstore排名前十的程序员应用软件
程序员又名程序猿,苦逼劳累的代名词,曾经一个朋友这么开玩笑说,如果你是富二代,你当程序员就是脑残,如果你是穷二代,当程序员的话,死的时候一定是趴键盘. 程序员 哦,可怜的程序员.在那山的这边海的那边有 ...
- Stack Overflow 上排名前十的与API相关的问题
Stack Overflow是一个庞大的编程知识仓库,在Stack Overflow 上,数百万的提问被回答,并且这些回答都是高质量的.这就是为什么在Google搜索结果的排行榜上,Stack Ove ...
- Top 10 Questions about Java Exceptions--reference
reference from:http://www.programcreek.com/2013/10/top-10-questions-about-java-exceptions/ This arti ...
- Vue(二十七)当前GitHub上排名前十的热门Vue项目(转载)
原文地址:https://my.oschina.net/liuyuantao/blog/1510726 1. ElemeFE/element tag:vue javascript components ...
- 【Java学习笔记之二十二】解析接口在Java继承中的用法及实例分析
一.定义 Java接口(Interface),是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为( ...
- Top 10 questions about Java Collections--reference
reference from:http://www.programcreek.com/2013/09/top-10-questions-for-java-collections/ The follow ...
- Java中的equals和hashCode方法
本文转载自:Java中的equals和hashCode方法详解 Java中的equals方法和hashCode方法是Object中的,所以每个对象都是有这两个方法的,有时候我们需要实现特定需求,可能要 ...
- 关于java中的hashcode和equals方法原理
关于java中的hashcode和equals方法原理 1.介绍 java编程思想和很多资料都会对自定义javabean要求必须重写hashcode和equals方法,但并没有清晰给出为何重写此两个方 ...
随机推荐
- Python异常处理,将异常写入到一个文件
'''定义一个函数func(urllist) urllist:为URL的列表,例如:['http://xx.com','http://www.xx.com','http://www.xxx.com'. ...
- div 3 frog jump
There is a frog staying to the left of the string s=s1s2…sn consisting of n characters (to be more p ...
- Prism 源码解读1-Bootstrapper和Region的创建
介绍 之前也研究过Prism框架但是一直没有深入理解,现在项目上想把一个Winform的桌面应用程序改造成WPF程序,同时我希望程序是可测试可维护架构良好的,Prism的这些设计理念正好符合我的需求, ...
- Redis 缓存更新一致性
当执行写操作后,需要保证从缓存读取到的数据与数据库中持久化的数据是一致的,因此需要对缓存进行更新. 因为涉及到数据库和缓存两步操作,难以保证更新的原子性. 在设计更新策略时,我们需要考虑多个方面的问题 ...
- 为什么要在离线A/B测试中使用贝叶斯方法
当涉及到假设检验时,贝叶斯方法可以取代经典的统计方法.这里将使用web分析的具体案例来演示我们的演示. 贝叶斯方法在经典统计中的重要性在此链接. https://towardsdatascience. ...
- (线段树 -星星等级)Stars POJ - 2352
题意: 给出n个星星的坐标 x,y ,当存在其他星星的坐标x1,y1满足x>=x1&&y>=y1时 这个星星的等级就加1. 注意: 题中给的数据是有规律的 ,y是逐渐增加的 ...
- 为什么我的PayPal 买家账号往商家账号付款,反而从商家账号里面扣款?
如果读者踩了跟我一样坑的话,建议赶紧去检查一下 application.properties 配置文件!!! 解决方法: 不妨试试将上面的 clientId和clientSecret 切换为你的商 ...
- IntelliJ Idea 中文乱码问题
首先,Idea真的是一款很方便的开发工具,但是关于中文乱码这个问题我不得不吐槽,这个编码也弄得这么麻烦干嘛呀...下面就说一下怎么解决中文乱码问题: 1.首先是编辑器的乱码,这个很好解决,file-& ...
- Java中的集合类、Lambda、鲁棒性简述
集合类 在java.util包中提供了一些集合类,常用的有List.Set和Map类,其中List类和Set类继承了Collection接口.这些集合类又称为容器,长度是可变的,数组用来存放基本数据类 ...
- while与until
一.格式: while 条件测试 :do 循环体 done 二.条件测试 条件测试是指满足条件则会一直执行下去. 比如: let i =0 while i < 100;do echo $i i ...