All About JAVA 将数组转换成列表Example

 

 /*
    Create List from Java Object Array Example 利用数组创建List的示例
    This java example shows how to create a List from an array of type Object using
    这个Java示例展示了如何使用Arrays的asList方法来利用一个数组生成List对象
    asList method of Arrays class.
  */ 
   
  import java.util.Arrays
  import java.util.List;
  import java.util.Iterator;
   
  public class CreateListFromObjectArrayExample{
   
  public static void main(String[] args){
  //create an array of type Object, in this case we will create String array、
  //创建一个数组类型对象,在这个示例中我们创建一个字符串数组
  String[] strArray= new String[]{"Object","Array","Converted","To","List"};
   
  /*
    To create List from an array of type Object use,
    static List asList(Object[] objArray) method of Arrays class.
    
    This method returns a fixed sized list backed by original array.
  为了利用一个数组对象初始化创建List,我们使用 Arrays类中的asList()这个静态方法。
  这个方法返回一个与原始数组对象相同长度的List
    */
   
  List list = Arrays.asList(strArray);
   
  //get an iterator 取得一个迭代器
  Iterator itr = list.iterator();
   
  //iterate through list created from Array 遍历使用数组生成的list
  System.out.println("List created from an Array of type Object contains,");
  while(itr.hasNext())
  System.out.println(itr.next());
   
  } 
  }
   
  /*
  Output would be 输出为:
  List created from an Array of type Object contains,
  Object
  Array
  Converted
  To
  List
  */


 

这个实例通过Arrays.asList()方法将一个数组转换成了List,但是需要注意,这个生成的List不是java.util.ArrayList而是Arrays的内部类。所以不能直接在其上执行add()等java.util.ArrayList才有的方法。

参考:http://blog.csdn.net/wanbin021614/article/details/1771384

原文:http://www.java-examples.com/create-list-java-object-array-example

可以使用JavaArrays类中的copyOf()和System.arraycopy()方法来合并多个String数组。 方法1:使用copyOf()方法 ```java String[] arr1 = {"Java", "is", "cool"}; String[] arr2 = {"I", "love", "it"}; String[] arr3 = {"How", "about", "you?"}; // 计算新数组的长度 int totalLength = arr1.length + arr2.length + arr3.length; // 创建新数组 String[] result = Arrays.copyOf(arr1, totalLength); // 将arr2和arr3合并到result中 System.arraycopy(arr2, 0, result, arr1.length, arr2.length); System.arraycopy(arr3, 0, result, arr1.length + arr2.length, arr3.length); System.out.println(Arrays.toString(result)); // [Java, is, cool, I, love, it, How, about, you?] ``` 方法2:使用System.arraycopy()方法 ```java String[] arr1 = {"Java", "is", "cool"}; String[] arr2 = {"I", "love", "it"}; String[] arr3 = {"How", "about", "you?"}; // 计算新数组的长度 int totalLength = arr1.length + arr2.length + arr3.length; // 创建新数组 String[] result = new String[totalLength]; // 将arr1、arr2和arr3合并到result中 System.arraycopy(arr1, 0, result, 0, arr1.length); System.arraycopy(arr2, 0, result, arr1.length, arr2.length); System.arraycopy(arr3, 0, result, arr1.length + arr2.length, arr3.length); System.out.println(Arrays.toString(result)); // [Java, is, cool, I, love, it, How, about, you?] ``` 上述两种方法都可以实现多个String数组的合并。需要注意的是,第一种方法使用了Arrays.copyOf()方法创建新数组,而第二种方法则直接使用了new关键字创建了一个新的数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值