【翻译】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,输出,内存地址:[[email protected]
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);
}

 

 

原文地址:https://www.cnblogs.com/caoRM/p/8793512.html

时间: 2024-10-08 04:09:51

【翻译】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 following are top 10 methods for Java Array. They are the most voted questions from stackoverflow. 0. Declare an array String[] aArray = new String[5

排名前十的开源安全项目

搜寻安全相关的开源软件最好的地方显然是 GitHub.你可以使用该网站上的搜索功能来找到这些有用的工具,但是有一个地方可以让你找到那些安全方面最流行的项目,那就是 GitHub 的展示区,可能知道它的人不多. 从 2014 年开始,GitHub 展示区就会按照分类陈列这些最流行的项目.在展示区中,项目以其所获得的星标数排名,这个列表会不断更新当前最流行的项目.展示区包含的"安全"分类中包含了 24 个项目,这里我们列出了在 GitHub 上排名前十的安全项目. 1. osquery -

全国高校学科评估:各专业排名前十的高校

全国高校学科评估:各专业排名前十的高校 考试点考研2016-03-07 17:06:08考研资讯 考研辅导 研院信息阅读(33332)评论(2) 声明:本文由入驻搜狐公众平台的作者撰写,除搜狐官方账号外,观点仅代表作者本人,不代表搜狐立场.举报 文章来源于考试点 转载请注明出处 2017考研已经逐渐提上日程,择校择专业也成为许多同学关心的头等大事.现下各种各种的高校排名不在少数,各式榜单很容易使人眼花缭乱.下面是教育部公布的各专业排名前十的高校,也是目前较为权威可信的高校专业评估,大家可以以此做

Top 10 questions about Java Collections--reference

reference from:http://www.programcreek.com/2013/09/top-10-questions-for-java-collections/ The following are the most popular questions of Java collections asked and discussed on Stackoverflow. Before you look at those questions, it's a good idea to s

Top 10 tough core Java interview questions answers programming

Tough core Java interview questions and answersWhat is tough core java interview question ? Why do people look for tough Java questions before going for interview? well I don't thing I need to answer these tough questions because its pretty natural t

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

Stack Overflow是一个庞大的编程知识仓库,在Stack Overflow 上,数百万的提问被回答,并且这些回答都是高质量的.这就是为什么在Google搜索结果的排行榜上,Stack Overflow 总是位居首位. 虽然Stack Overflow上有非常多的提问,但是仍然每天都有大量的问题被提出,其中的很多都等待解答或者没有得到好的解答.因此,问题是如何找到答案的,通过Stack Overflow是不够的. 随着成千上万的开发者使用Java的API并且在Github上分享他们的项目

2016年度中国会计师事务所排名前十

http://www.wenxuetiandi.com/news/xuexizixun/201708/444478.html 1普华永道 普华永道(Price Waterhouse Coopers)是四大国际会计师事务所之一,主要服务领域包括审计.税务.人力资源.交易.危机管理等.普华永道通过制定解决方案及提供实用性意见,不断为客户及股东提升价值.普华永道致力于提供切合各行业所需要的审计.税务及谘询服务,以提升客户的价值.普华永道在158 个国家和地区超过 180,000 人的专业团队所组成的全

Top 10 Questions about Java Exceptions--reference

reference from:http://www.programcreek.com/2013/10/top-10-questions-about-java-exceptions/ This article summarizes the top 10 frequently asked questions about Java exceptions. 1. Checked vs. Unchecked In brief, checked exceptions must be explicitly c

AD统计,排名前十的国家每年的论文统计量

1.获取每个国家的论文数量,采取的方法是写好sql语句,直接用sql语句统计数量,可能这种方式速度会比较慢,另外一种方法是把id全部传过来,在本地做统计. import pymysql import json import re import collections import json def get_article_from_mysql(sql): conn= pymysql.connect( host='localhost', port = 3306, user='root', pass