简体   繁体   English

将整数添加到ArrayList <Integer>

[英]Adding Integers to ArrayList<Integer>

I have an ArrayList of LinkedList s (an array of linked lists). 我有一个LinkedListArrayList (一个LinkedList的数组)。 The LinkedList s contains integers ( Integer ). LinkedList包含整数( Integer )。

private List<LinkedList> buckets;
buckets = new ArrayList<LinkedList>();
for (int i = 0; i < 10; i++) {
    LinkedList<Integer> temp = new LinkedList<Integer>();
    buckets.add(temp);
}

I later want to remove the items from the linked list (in the order they were added) and add them to an array list. 我后来想要从链表中删除项目(按添加顺序)并将它们添加到数组列表中。 When I try this: 当我尝试这个:

ArrayList<Integer> sorted = new ArrayList<Integer>(unsorted.size());
for (int i = 0; i < buckets.size(); i++) {
    for (int j = 0; j < buckets.get(i).size(); j++) {
        sorted.add(buckets.get(j).removeLast());
        // sorted.add((Integer)buckets.get(j).removeLast());
    }
}

I get an error saying: 我收到一个错误说:

add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.Object)

But when I cast it to an Integer (the commented out line), the array is full of null values . 但是当我将它转换为Integer (注释掉的行)时,数组中充满了null values Anyone see what I am doing wrong? 有谁看到我做错了什么?

Here is where I am adding items to bucket : 这是我向bucket添加项目的地方:

for (int i = 0; i < unsorted.size(); i++) {
    int digit = (unsorted.get(i) / position) % 10;
    buckets.get(digit).add(unsorted.get(i));
}

Note that sorted is an ArrayList<Integer> . 请注意, sorted是一个ArrayList<Integer> When I trace it in debug mode, I can see that the LinkedLists have Integer objects with the correct values. 当我在调试模式下跟踪它时,我可以看到LinkedLists具有正确值的Integer对象。

Screenshot of buckets contents: 存储桶内容的屏幕截图:

存储桶内容调试输出

Working Example: 工作实例:

class Ideone {
    private static List<LinkedList<Integer>> buckets;
    public static void main (String[] args) throws Exception {
        ArrayList<Integer> arr = new ArrayList<Integer>();
        arr.add(6);
        arr.add(8);
        arr.add(1);
        arr.add(3);
        arr.add(9);
        System.out.println(arr);
        arr = sort(arr);
        System.out.println(arr);
    }
    public static ArrayList<Integer> sort(ArrayList<Integer> unsorted) {
        buckets = new ArrayList<LinkedList<Integer>>();
        for (int i = 0; i < 10; i++) {
            LinkedList<Integer> temp = new LinkedList<Integer>();
            buckets.add(temp);
        }
        ArrayList<Integer> sorted = new ArrayList<Integer>(unsorted.size());
        for (int i = 0; i < unsorted.size(); i++) {
            int digit = unsorted.get(i) % 10;
            buckets.get(digit).add(unsorted.get(i));
        }
        for (int i = 0; i < buckets.size(); i++) {
            for (int j = 0; j < buckets.get(i).size(); j++) {
                sorted.add(buckets.get(j).poll());
                // sorted.add((Integer)buckets.get(j).removeLast());
            }
        }
        return sorted;
    }
}

You are using the raw form of LinkedList here: 您在这里使用LinkedList的原始形式:

private List<LinkedList> buckets;

Because of this, removeLast will return Object , not Integer . 因此, removeLast将返回Object ,而不是Integer Try 尝试

private List<LinkedList<Integer>> buckets;

and

buckets = new ArrayList<LinkedList<Integer>>();

Casting the return of removeLast to Integer was the pre-generics way of getting this to work. removeLast的返回转换为Integer是使这种方法起作用的前泛型方法。 However, you never inserted any items into each LinkedList , so removeLast returns null . 但是,您从未在每个LinkedList插入任何项,因此removeLast返回null If you want something returned, first insert something into each LinkedList that gets inserted into buckets . 如果你想要返回的东西,首先在插入到buckets每个LinkedList中插入一些东西。

Casting to Integer would still work, but supplying Integer as the type argument to LinkedList is preferred, especially since you are using generics by supplying LinkedList as the type parameter to List already. 转换为Integer仍然有效,但是首选将Integer作为LinkedList的类型参数提供,尤其是因为您通过将LinkedList作为类型参数提供给List已经使用泛型。

In your nested loop, 在你的嵌套循环中,

  for (int i = 0; i < buckets.size(); i++) {
     for (int j = 0; j < buckets.get(i).size(); j++) {

        // ***** here *****
        sorted.add(buckets.get(j).poll());

     }
  }

You look to be polling the wrong List. 您希望轮询错误的列表。

Try changing 尝试改变

sorted.add(buckets.get(j).poll());

to: 至:

sorted.add(buckets.get(i).poll());

Perhaps a cleaner more intuitive way to code this would be something like: 也许更清晰,更直观的编码方式如下:

  for (int i = 0; i < buckets.size(); i++) {
     LinkedList<Integer> innerList = buckets.get(i);
     for (int j = 0; j < innerList.size(); j++) {
        sorted.add(innerList.poll());
     }
  }

Although this may not work if the innerList has multiple items. 虽然如果innerList有多个项目,这可能不起作用。 Why not instead remove items safely with an iterator? 为什么不用迭代器安全地删除项目?

  for (int i = 0; i < buckets.size(); i++) {
     LinkedList<Integer> innerList = buckets.get(i);
     for (Iterator<Integer> iterator = innerList.iterator(); iterator.hasNext();) {
        sorted.add(iterator.next());
        iterator.remove(); // this guy is optional
     }
  }

Either that or simply use get(j) 或者只是use get(j)

  for (int i = 0; i < buckets.size(); i++) {
     LinkedList<Integer> innerList = buckets.get(i);
     for (int j = 0; j < innerList.size(); j++) {
        sorted.add(innerList.get(j));
     }
  }

Although this isn't efficient use of a LinkedList 虽然这不是有效使用LinkedList

The item that you inserted into the ArrayList "sorted" is the item you took from the link list LinkedList. 您插入到ArrayList“sorted”中的项目是您从链接列表LinkedList中获取的项目。

But you never actually add any item to it. 但你从来没有真正添加任何项目。 You simply just created a LinkedList and added it to your bucket list. 您只需创建一个LinkedList并将其添加到您的存储桶列表中。

You need to add something into the temp list. 您需要在临时列表中添加一些内容。

for (int i = 0; i < 10; i++) {
    LinkedList<Integer> temp = new LinkedList<Integer>();
    // Add something to the temp LinkedList
    buckets.add(temp);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM