6 Replies Latest reply: May 27, 2007 1:45 PM by 807600 RSS

    ArrayIndexOutOfBoundsException

    807600
      Hi. I am trying to create a hashmap as follows:

      [ [1],[2,3,4] ]
      [ [5],[6,7,8] ]

      However the following code is producing an ArrayIndexOutOfBoundsException at the inner for loop:
      String arr [  ] = { "1","2","3","4" } ;
      String arr2 [  ] = { "5","6","7","8" } ;
      
      arraylist.add ( arr);
      arraylist.add ( arr2 ) ;
      
      String[] s = null;
      Integer[] value = new Integer[2];
      HashMap hm = new HashMap();
      for ( int i=0;i < arraylist.size (  ) ;i++ ){
              s = (String[])arraylist.get(i);
           Object key = Integer.valueOf( s[0] );
           for ( int i2=1;i2 < s.length ;i2++ ){
                value[i2 - 1] = Integer.valueOf( s[i2] );
           }
           hm.put(key,value);
      }
        • 1. Re: ArrayIndexOutOfBoundsException
          807600
          Hi,

          In your second for-loop you are iterating over value "s.length" which is 4, the array you are working on "value" only has a length of 2. I don't follow exactly what you are trying to do so I can't offer a solution but the above is your problem.
          try running the code below:
          String arr [  ] = { "1","2","3","4" } ;
                    String arr2 [  ] = { "5","6","7","8" } ;
                     
                    arraylist.add ( arr);
                    arraylist.add ( arr2 ) ;
                     
                    String[] s = null;
                    Integer[] value = new Integer[2];
                    HashMap hm = new HashMap();
                    for ( int i=0;i < arraylist.size (  ) ;i++ ){
                            s = (String[])arraylist.get(i);
                         Object key = Integer.valueOf( s[0] );
                         System.out.println("Value lenght: "+value.length);
                         System.out.println("S length: "+s.length);
                         for ( int i2=1;i2 < s.length ;i2++ ){
                              System.out.println("Iteration: "+i2);
                              value[i2 - 1] = Integer.valueOf( s[i2] );
                         }
                         hm.put(key,value);
          Message was edited by:
          helloWorld
          • 2. Re: ArrayIndexOutOfBoundsException
            807600
            i changed the length of value array to 3 and works fine now. thanks
            • 3. Re: ArrayIndexOutOfBoundsException
              807600
              is there a quicker way of printing out the contents of an arraylist other than the method i have been using below
              public static String arrayToString(Object array) {
                         if (array == null) {
                           return "[NULL]";
                         } else {
                           Object obj = null;
                           if (array instanceof Hashtable) {
                             array = ((Hashtable)array).entrySet().toArray();
                           } else if (array instanceof HashSet) {
                            array = ((HashSet)array).toArray();
                          } else if (array instanceof Collection) {
                            array = ((Collection)array).toArray();
                          }
                          int length = Array.getLength(array);
                          int lastItem = length - 1;
                          StringBuffer sb = new StringBuffer("[");
                          for (int i = 0; i < length; i++) {
                            obj = Array.get(array, i);
                            if (obj != null) {
                              sb.append(obj);
                            } else {
                              sb.append("[NULL]");
                            }
                            if (i < lastItem) {
                              sb.append(", ");
                            }
                         }
                          sb.append("]");
                          return sb.toString();
                       }
                }
              • 4. Re: ArrayIndexOutOfBoundsException
                807600
                Yep. If you just need to print the contents of an ArrayList you can do this:
                import java.util.*;
                
                public class ArrayTest {
                     
                     public static void main(String[] args) {
                          
                          List list = new ArrayList();
                          
                          list.add("String one");
                          list.add("String two");
                          
                          System.out.println(list);  //prints with brackets
                          System.out.println("-----------");
                          for (int i = 0; i < list.size(); i++) {
                               System.out.println(list.get(i)); // prints the String only at index i
                          }
                          
                     }
                }
                EDIT: Do you mean to print out the contents of the Collection when you do not know it's type? ie if it is a List, Set etc?

                Message was edited by:
                helloWorld
                • 5. Re: ArrayIndexOutOfBoundsException
                  807600
                  Well I'm trying to print the contents of an array within an arraylist as in the example above with the string arrays
                  • 6. Re: ArrayIndexOutOfBoundsException
                    807600
                    This is another way. There could be a smarter way than this also, not sure of the top of my head.
                    import java.util.*;
                    
                    public class ArrayTest {
                         
                         public static void main(String[] args) {
                              
                              List list = new ArrayList();
                              String [] strings1 = {"String one", "String two" };
                              String [] strings2 = {"String one", "String two","String three" };
                              list.add(strings1);          
                              list.add(strings2);
                              printContents(list);
                         }
                         
                         static void printContents(List list) {
                              String [] strings;
                              
                              for (int i = 0; i < list.size(); i++) {
                                   strings = (String [])list.get(i);
                                   System.out.println("Array At Element:" +i);
                                   for (int j = 0; j < strings.length; j++) {
                                        System.out.println(strings[j]);
                                   }
                         
                              }
                         }
                    }