1

I have an ArrayList which contains some unique Strings sorted alphabetically.

I want to store the index of the String in the list, which starts with the character in an array which has 27 slots corresponding to each alphabetic character and one extra for the other characters.

For eg.

1 mandown
13 days
after
between
company
family
far
home
important
live
lot
mean
might
national
next
over
same
small
so
still
talk
though
too
zachs

So when I iterate through this list, If I find a word that starts in char 'a' it should put the index of that word in the list in my array[0].. like that, If I find a word that starts in char 'b' it should put the index of that word in the list in my array[1] and so on till char 'z' in array[25] and if it starts with any other character, it should put the index of that character in array[26].

If it cant find a word in the list which starts in a particular character, use the index of the last found character.

So after doing the logic,

array[0] = 2 // A
array[1] = 3 // B
array[2] = 4 // C
array[3] = 4 // D, No word for D in the list so taking index of 'c'
array[4] = 4 // E, No word for E in the list so taking index of 'c'
array[5] = 6 // F
array[6] = 6 // G, No word for D in the list so taking index of 'f'
.
.
.
array[24] = index of last found word, ie. 't'
array[25] = 23 // Z
array[26] = 1 // numeric characters

The logic I have n my mind is

public static int [] calculateIndexForAlphabets(List libList)
{
    int[] array = new int[27];
    for (int x = libList.size() - 1; x >= 0; x--)
    {
        Item lmi = (Item) libList.get(x);

        String name = lmi.getName(); //my method to return the name
        if (name != null)
        {
            // Not sure what to do inside  
            // thinking of something like taking the name.toLowerCase().charAt(0) and using that
        }
    }
    return array;
}  

Please provide some insights into this.

Thanks,
Sen

3
  • "If it cant find a word in the list which starts in a particular character, use the index of the last found character." What happens if there's no word that starts in "a"? Jan 12, 2014 at 11:49
  • if it has numeric characters then the index of that word otherwise it should default to 0.. Jan 12, 2014 at 11:50
  • I have posted the answer here. stackoverflow.com/a/21697018/449378 Feb 11, 2014 at 8:54

1 Answer 1

1

You're on the right track. Once you have the first char, check if it's between 'a' and 'z', and if it is, subtract 'a' from it to find the index. If it isn't, the index is 26.

In the end, fill the missing indices by iterating through the result.

5
  • Is it possible, like, since this is a alphabetically sorted list, if I found one in 'a' and then I have some 15 words starting in 'a' and then the next word in 'b', can I skip the 15 and jump to 'b' and so on. Jan 12, 2014 at 11:54
  • You could use a binary search instead of iterating until finding a word starting with something other than 'a', but unless your list is huge, you won't gain much by doing so, and the logic will be more complex. Make a correct program first. Then optimize if needed.
    – JB Nizet
    Jan 12, 2014 at 11:58
  • I was actually referring to this question : stackoverflow.com/questions/21048966/… Jan 12, 2014 at 12:00
  • The above question is the real reason why this question came into my mind. :-) Jan 12, 2014 at 12:01
  • That's what binary search is for: docs.oracle.com/javase/7/docs/api/java/util/…. But 1000 is a tiny number for a computer. And since you have to iterate anyway, you won't gain much by simply skipping words.
    – JB Nizet
    Jan 12, 2014 at 12:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.