2

Is there any way I can make the below code work without commenting the 3rd line.

    List<Integer> list = new ArrayList<Integer>();
    list.add(0,0);
    //list.add(1,null);
    list.add(2,2);

I want to add items to list at specific locations. But if I don't change the index to Nth position I am not being able to add at Nth as told in this answer.

I can't use a map because I don't want to miss a value when the keys are same. Also adding null values to a list for large lists will be an overhead. When there is a collision I want the item to take the next position(nearest to where it should have been).

Is there any List implementation that shifts index before it tries to add the item?

12
  • Why not use a Map<Integer, List<Integer>>? Commented Oct 11, 2011 at 16:47
  • Use a TreeMap since that allows iteration of the keys in order. Commented Oct 11, 2011 at 16:48
  • What's the desired behaviour when you have a collision at a key? Commented Oct 11, 2011 at 16:50
  • 1
    @MartijnCourteaux: I'm not sure. His mention of "can't use a map because I don't want to miss a vlaue when the keys are the same", suggested that he wanted multiple values for one key, but I could be wrong. As some have mentioned, the original post is not very clear about this and the original poster appears to be in no hurry to clarify this. Commented Oct 11, 2011 at 16:59
  • 1
    isn't that a case for an Array? Commented Oct 11, 2011 at 17:00

4 Answers 4

5

Use something like a MultiMap if your only concern is not "missing a value" if the keys are the same.

I'm not sure how doing a shift/insert helps if I understand your problem statement--if the "key" is the index, inserting will lose the same information.

Sign up to request clarification or add additional context in comments.

1 Comment

Specifically, a TreeMultimap provides in-order iteration of the keys.
3

You can use Vector and call setSize to prepopulate with null elements.

However, your comment about the overhead of the nulls speaks to an associative container as the right solution.

1 Comment

I think we need setSize() method in List also.It can be useful.
2

This still smells like you should be using a Map. Why not use a Map<Integer, List<Integer>>?

something like,

   private Map<Integer, List<Integer>> myMap = new HashMap<Integer, List<Integer>>();

   public void addItem(int key, int value) {
      List<Integer> list = myMap.get(key);
      if (list == null) {
         list = new ArrayList<Integer>();
         myMap.put(key, list);
      }
      list.add(value);
   }

   public List<Integer> getItems(int key) {
      return myMap.get(key);
   }

Comments

2

Well, There are a couple of ways I would think to do this, if you are not adding items too frequently, then it might be a good idea to simply do a check to see if there is an item at that location before adding it.

if(list.get(X) == null)
{
 list.add(X,Y);
}

Otherwise if you are going to be doing this too often...then I would recommend creating your own custom List class, and extending ArrayList or whatever you are using, and simply override the add method, to deal with collisions.

2 Comments

Yes. I wanted the check to happen automatically and increase the size if it is less. Looks like I need to think about a custom list implementation to do it.
this simply does not work at all, since java.util.ArrayList#add(int, E) does do range check: if (index > size || index < 0) So for example you cannot add element at first index into empty list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.