2

I have a problem with adding String to a ArrayList>. Which is declared

ArrayList<LinkedList<String>> table = new ArrayList<>();

In the constructor of the class I call ArrayLists ensureCapacity method, which doesn't seem to do what I was expecting. I was thinking it would increase the slots for LinkedList inside it, but it just remains empty.

When I try to call my add method:

    public boolean add(String s)
{
    boolean wanted = true;
    if(false)
    {
        wanted = false;
    }
    int index = Math.abs(s.hashCode() % table.size());
    table.ensureCapacity(index);
    table.get(index).addFirst(s);
    return wanted;
}

Since the table size doesn't get increased by ensureCapacity, I get java.lang.ArithmeticException: by zero. How can I work around this? I pictured this in my head as the ArrayList being the first column in a table, and LinkedLists being the rows. So each slot in the ArrayList references a LinkedList which Strings would be saved in. But since the ArrayList is empty at start and I can't increase with ensureCapacity and using the % operator in the add method it won't increase itself.

4
  • 1
    size() returns the number of items in the array list, not the capacity. This will be 0 on the first time, so the MOD (%) operation throws Divide by Zero Commented Feb 9, 2015 at 11:32
  • I would assume that table.size() is 0 and giving you the exception your seeing. Maybe put a check that says if(table.size() == 0) { /*increase table size*/ } Commented Feb 9, 2015 at 11:32
  • Aha! I thought capacity was the same thing as the table size @Bret. Commented Feb 9, 2015 at 11:39
  • Yes table.size() is 0, ill try that @ug_ but what will happen if i increase the table size by too little? I want to keep my linkedlists short, is there a way to continously increase the size when lists start getting large? Commented Feb 9, 2015 at 11:41

2 Answers 2

1

ensureCapacity() may resize the backing array of the ArrayList, but is doesn't change the number of elements actually stored in the List. Therefore, it doesn't affect the return value of table.size().

table.get(index) will return null unless you add at least index+ elements to your list.

It looks like you are trying to implement something similar to HashMap (though HashMap uses an array and not an ArrayList).

The only way you can rely of table.size() is if you add n empty LinkedLists to table. Then table.size() would return n, and that would be the size of your table.

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

1 Comment

That is exactly what I'm trying to do. I'm taking a class and to create one was this weeks assignment. After talking to some fellow classmates, they seem to have all set a certain amount of lists in the constructor, I guess I'll try that aswell. Would be cool to make it increase on it's own after a while though.
1

Just fill the list while it is not big enough with empty LinkedLists

while (table.size() < EXPECTED_SIZE) {
 table.add(new LinkedList<String>();
}

Comments

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.