2

我不太确定如何正确地表达这个词。我有一个对象列表,这些对象具有特定字段的吸气剂。我现在需要从 ojbect 列表中创建一个数组,但我只需要一个特定的数据。

有没有办法在不使用看起来非常低效的交互器的情况下做到这一点?

这是在 android 应用程序的上下文中。

4

2 回答 2

0
    // our original list
    List<Integer> list = new ArrayList<Integer>();

    // inserting some values
    for(int i = 0;i<100;i++){
        list.add(i);
    }

    // work starts here :

    // select a range of elements based on index
    List<Integer> subList = list.subList(0, 50);
        // list.subList(from index-inclusive,to index-exclusive)

    // create an array to hold your new values
    Integer[] myArray = new Integer[0]; // must initialize

    // assign the part of your original list to this array
    myArray = subList.toArray(myArray);


    // test Result :
    System.out.println(Arrays.toString(myArray));
        // reult :
        /*
         * [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
         * 11, 12, 13, 14, 15, 16, 17, 18, 19, 
         * 20, 21, 22, 23, 24, 25, 26, 27, 28, 
         * 29, 30, 31, 32, 33, 34, 35, 36, 37, 
         * 38, 39, 40, 41, 42, 43, 44, 45, 46, 
         * 47, 48, 49]
         */

    // hope this was what you were looking for   
于 2013-09-19T21:33:57.420 回答
0

只是为了解决这个问题 - 问题下方的评论是正确的“答案”。似乎没有更好(或更有效)的方式来做我要求的事情。

于 2013-09-24T15:42:29.863 回答