1.  声明一个数组

Java代码:

 String[] aArray = new String[5];
String[] bArray = {"a","b","c", "d", "e"};
String[] cArray = new String[]{"a","b","c","d","e"};

2.  输出一个数组

Java代码:

 int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray); // print directly will print reference value
System.out.println(intArray);
// [I@7150bd4d System.out.println(intArrayString);
// [1, 2, 3, 4, 5]

3.  从一个数组创建数组列表

Java代码:

 String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
System.out.println(arrayList);
// [a, b, c, d, e]

4.  检查一个数组是否包含某个值

Java代码:

 String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true

5.  连接两个数组

Java代码:

 int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

6.  声明一个内联数组(Array inline)

Java代码:

 method(new String[]{"a", "b", "c", "d", "e"});  

7.  把提供的数组元素放入一个字符串

Java代码:

 // containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);
// a, b, c

8.  将一个数组列表转换为数组

Java代码:

 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);

9.  将一个数组转换为集(set)

Java代码:

 Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]

10.  逆向一个数组

Java代码:

 int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]

11.  移除数组中的元素

Java代码:

 int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));

12.  将整数转换为字节数组

Java代码:

 byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();  

 for (byte t : bytes) {
System.out.format("0x%x ", t);
}

操作 Java 数组的 12 个最佳方法的更多相关文章

  1. 关于 Java 数组的 12 个最佳方法

    1.  声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...

  2. Java 数组的 12 个最佳方法

    1.  声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...

  3. Java数组的12个常用方法

    以下是12个关于Java数组最常用的方法,它们是stackoverflow得票最高的问题. 声明一个数组 String[] aArray = new String[5]; String[] bArra ...

  4. Java 数组的三种创建方法,数组拷贝方法

    public static void main(String[] args) {//创建数组的第一种方法int[] arr=new int[6];int intValue=arr[5];//Syste ...

  5. java - 数组与String的length方法问题

    java数组没有length()方法,java数组有length属性: String有length()方法.

  6. Java 数组的三种创建方法

    public static void main(String[] args) { //创建数组的第一种方法 int[] arr=new int[6]; int intValue=arr[5]; //S ...

  7. Java 数组的 12 个方法

    1.  声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...

  8. Java数组拷贝的五种方法

    在Java中有多种方法可以拷贝一个数组,到另外一个数组. 1.循环拷贝 在循环拷贝方法中,只需要利用i,移动指针即可复制所有数组到arrayB中. for(int i=0;i<arrayA.le ...

  9. [数据库操作]Java中的JDBC的使用方法.

    前言:想必大家在实际编码中都遇到过JDBC的操作, 这里仅做自己的一个总结, 有错误和不完整之处还请大家提出来. 1,JDBC其实一套规范(接口)数据库厂商需要实现此接口(实现类)--数据库驱动 2, ...

随机推荐

  1. OC与JS交互之UIWebView

    随着H5的强大,hybrid app已经成为当前互联网的大方向,单纯的native app和web app在某些方面显得就很劣势.关于H5的发展史,这里有一篇文章推荐给大家,今天我们来学习最基础的基于 ...

  2. 定时备份oracle数据库脚本文件

    @echo off REM ########################################################### REM # Windows Server 2003下 ...

  3. 收藏 创建第一个mvc

    http://blog.csdn.net/sdtsfhh/article/details/8201956

  4. HTML三种样式引入方式

    HTML三种样式引入方式   HTML有三种样式引入方式:行内样式(inline Styles).嵌入式样式表(Embedded Style Sheets).外部样式表(External Style ...

  5. (二)JavaScript之[函数]与[作用域]

    3].函数 /** * 事件驱动函数. * 函数执行可重复使用的代码 * * 1.带参的函数 * 2.带返回值的函数 * 3.局部变量 * * 4.全局变量 * 在函数外的:不用var声明,未声明直接 ...

  6. react-native 视频播放器(很不错哦)

    第一步: npm i -S react-native-af-video-player(安装前:先安装: react-native-video.react-native-keep-awake.react ...

  7. mvc中尽量避免使用HttpContext.Current.Request

    在Mvc开发中滥用HttpContext.Current.Request,可能会造成非IE浏览器重复加载页面的情况. 不管你信不,反正我在Mvc3.0中遇到过.

  8. C#设计模式--工厂模式(创建型模式)

    一.简单工厂模式(UML类图): 核心类代码: public class Calc { public double NumberA { get; set; } public double Number ...

  9. HttpWebRequest Post请求webapi

    1.WebApi设置成Post请求在方法名加特性[HttpPost]或者方法名以Post开头如下截图: 2.使用(服务端要与客户端对应起来)[单一字符串方式]:注意:ContentType = &qu ...

  10. python定义class

    python也是面向对象的语言,类的重要性不言而喻. class Animal: def __init__(self,voice='hello'): self.voice=voice def __de ...