这里列举了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)的更多相关文章

  1. Top 10 Methods for Java Arrays

    作者:X Wang 出处:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/ 转载文章,转载请注明作者和出处 The ...

  2. Appstore排名前十的程序员应用软件

    程序员又名程序猿,苦逼劳累的代名词,曾经一个朋友这么开玩笑说,如果你是富二代,你当程序员就是脑残,如果你是穷二代,当程序员的话,死的时候一定是趴键盘. 程序员 哦,可怜的程序员.在那山的这边海的那边有 ...

  3. Stack Overflow 上排名前十的与API相关的问题

    Stack Overflow是一个庞大的编程知识仓库,在Stack Overflow 上,数百万的提问被回答,并且这些回答都是高质量的.这就是为什么在Google搜索结果的排行榜上,Stack Ove ...

  4. Top 10 Questions about Java Exceptions--reference

    reference from:http://www.programcreek.com/2013/10/top-10-questions-about-java-exceptions/ This arti ...

  5. Vue(二十七)当前GitHub上排名前十的热门Vue项目(转载)

    原文地址:https://my.oschina.net/liuyuantao/blog/1510726 1. ElemeFE/element tag:vue javascript components ...

  6. 【Java学习笔记之二十二】解析接口在Java继承中的用法及实例分析

    一.定义 Java接口(Interface),是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为( ...

  7. Top 10 questions about Java Collections--reference

    reference from:http://www.programcreek.com/2013/09/top-10-questions-for-java-collections/ The follow ...

  8. Java中的equals和hashCode方法

    本文转载自:Java中的equals和hashCode方法详解 Java中的equals方法和hashCode方法是Object中的,所以每个对象都是有这两个方法的,有时候我们需要实现特定需求,可能要 ...

  9. 关于java中的hashcode和equals方法原理

    关于java中的hashcode和equals方法原理 1.介绍 java编程思想和很多资料都会对自定义javabean要求必须重写hashcode和equals方法,但并没有清晰给出为何重写此两个方 ...

随机推荐

  1. 暴力+辗转相除法——N个数求和

    题目来源 PTA 团体程序设计天梯赛-练习集 L1-009 N个数求和 (20分) https://pintia.cn/problem-sets/994805046380707840/problems ...

  2. abp(net core)+easyui+efcore实现仓储管理系统——入库管理之五(四十一)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  3. Xmind8 Pro破解版

    思维导图又叫心智导图,是表达发散性思维的有效图形思维工具 ,它简单却又很有效,是一种实用性的思维工具.思维导图运用图文并重的技巧,把各级主题的关系用相互隶属与相关的层级图表现出来,把主题关键词与图像. ...

  4. 3 report formats of SFDC

    Choose one of the following report formats using the Format menu of the report builder. Tabular form ...

  5. 使用Keras进行深度学习:(二)CNN讲解及实践

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 现今最主流的处理图像数据的技术当属深度神经网络了,尤其是卷积神经网 ...

  6. 性能计数器在.NET Core中的新玩法

    传统的.NET Framework提供的System.Diagnostics.PerformanceCounter类型可以帮助我们收集Windows操作系统下物理机或者进程的性能指标,基于Perfor ...

  7. CentOS76 安装k8s1.18的简单步骤

    1. 前提条件就不再详细描写了: 关闭防火墙 升级内核到至少4. 关闭swap 关闭selinux 等 2. 本次安装采用酸酸乳和privoxy的方式进行, 不过发现部分rpm 包还是特别慢,没办法用 ...

  8. 5.Maven坐标

    而这个坐标也意味着jar包等保存在 C:\Users\用户名.m2\repository\org\apache\tomcat\tomcat-catalina\9.0.2

  9. logstash用jdbc插件将数据库内容导入elasticsearch时间字段相差5小时

    logstash将mysql的数据导入elasticsearch之后发现时间字段的相差5个小时 解决办法: 在数据库连接配置后面加上?serverTimezone=UCT这个就OK了 logstash ...

  10. 使用内部枚举类作为外部类的参数的Mybatis的参数该如何判断

    新写了一个接口,期望根据不同的参数来给数据库中不同的字段进行传值.这里使用了内部静态枚举类的方式进行传值,在写mybatis动态sql时,如果是普通对象,一般使用,那么使用枚举类,如何判断枚举类的值呢 ...