0

Don't know why I'm getting this error. Working with ArrayLists and sorting words into appropriate ArrayLists by alphabetical order. If anyone can help me understand why I'm getting the error and how to fix it that would be great!

import java.util.*;

public class Sort {
    public static ArrayList<Object> sortByFirstLetter( List<String> words) {
        ArrayList<Object> bucket = new ArrayList<Object>();
        for( int i = 0; i < 26; i++ ) {
            ArrayList<String> letter = new ArrayList<String>();
            bucket.add(letter);
        }
        for( String word : words ) {
            int index = (int)(word.toLowerCase().charAt(0)) - 97; //get a number 0-25 for index; 97 is unicode for lwrcse "a"
            System.out.println(index);
            bucket.get(index).add(str);     //THIS LINE GIVES ERROR
        }
        return bucket;
    }

    public static void main(String[] args) {   
        List<String> words = Arrays.asList("alex", "andy", "kevin");
        sortByFirstLetter( words );
    }

}

2
  • What are you intending to add to what? You are currently trying to "add" to an Object, which is nonsensical. Commented Apr 8, 2015 at 23:54
  • Trying to add to the ArrayList<String> which is inside the ArrayList<Object> Commented Apr 8, 2015 at 23:55

1 Answer 1

2

It looks like bucket should be a List<List<String>>, not a List<Object>. It's not clear why you made it a List of Object in the first place.

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

1 Comment

Um, by writing List<List<String>> instead of List<Object>?

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.