hi ,
i am tryin to put all the upper case alphabets in an array list and , then i want it to print on screen , but for some particular reason i am getting no result but a strange output.
here is the code and result:

void buildAlphabet()
{
	ArrayList al = new ArrayList();

   for ( int i = 1 ; i<=26;i++)/***Goes accordin to the ASCII code*******/
   {
   	   al.add(i,(char)(i+64));
   }
   System.out.print("Contents of array : " +al);
}

result:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(ArrayList.java:367)
at alpha_freq.buildAlphabet(alpha_freq.java:13)

Any clue abt this ? , i will appreciate help thanks

Read the API doc for ArrayList.add(int,E) It states that will be thrown if index < 0 or index > size(). Your error message gives that information to you as well. How can you insert an element at index position 1 when size is 0? The indexes are zero-based, so you must insert the first element... (your answer here)

hi,
Thanks for the guidance : )

void buildAlphabet()
{
	ArrayList al = new ArrayList(26);
	al.add("A");//at i = 0

   for ( int i = 1 ; i<=26;i++)
   {
   	   al.add(i,(char)(i+65));
   }
   System.out.print("\nContents of array : " +al);
}

result:
Contents of array : [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, []
thanks ; )

Alternately, you could have just used the add(Object) signature without specifying the index, since you're entering them in order anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.