0

Say if I have a user input any word they want and I want to save that word to an Array named the first letter of the word they enter. I already initialize these Arrays ( a - z ). So when a user enters for example:

"Apple" and gets saved as word, then I do .substring(0,1).toLowerCase(); and save that as String Letter. How do I go about basically doing Letter.add(word);

At the moment I have 26 if statements and there MUST be an easier way of doing this.

Thank you.

Edit: What I have now is an ArrayList of ArrayLists because it was what our teacher wanted. I called the first Array List, and then the Arrays inside that are named a - z. In one of my methods I want to delete an entry by name. deleteEntry ( String name ). I then get the first letter of that name and put it to lowercase. Then I dont know how I can just do [firstLetter].remove(x) (where x is index of name)

Basically is it possible to do this.

(String).add(Another String);

where the String can only be a letter of the english alphabet.

3
  • 2
    ma y we see your code ;) Commented Apr 27, 2014 at 20:05
  • you have the word String word, and the letter. Perhaps you could use a swich-case (case "a": etc) Commented Apr 27, 2014 at 20:06
  • You should really post some more code if you want a more precise answer. I don't think it's clear what you have already and what you want. Commented Apr 27, 2014 at 21:10

2 Answers 2

2

You can simply use a Map<Character, List<String>>.

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

Comments

1

Define an array arr of ArrayLists of Strings and index this
array from 0 to say 255. The char type can be converted up to int
so you can use char as index of your array.

So just do arr[firstLetter].add(word) where
firstLetter is the char word.charAt(0).

1 Comment

What I have now is an ArrayList of ArrayLists because it was what our teacher wanted. I called the first Array List, and then the Arrays inside that are named a - z. In one of my methods I want to delete an entry by name. deleteEntry ( String name ). I then get the first letter of that name and put it to lowercase. Then I dont know how I can just do [firstLetter].remove(x) (where x is index of name)

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.