-4
import java.util.*;


public class Main {

    //This method is complete. Do not change it.
    public static void main(String[] args) {
        Random r = new Random();
        r.setSeed(System.currentTimeMillis());
        int n = r.nextInt(6) + 1;
        List[]a = new ArrayList[1];

        makeLists(a);

        System.out.println(a.length);
        for(int i = 0; i < a.length; i++)
            System.out.println(a[i].toString());
    }

    private static void makeLists(List[] a) {

         for (int i=0; i<a.length; i++){
      List<Character> chars = new ArrayList<Character>();
      chars.add(0,'A');
      chars.add(1,'B');
      chars.add(2,'C');
      chars.add(3,'D');
      chars.add(4,'E');
      chars.add(5,'F');
      chars.add(6,'G');
      chars.add(7,'H');
      chars.add(8,'I');
      chars.add(9,'J');
      chars.add(10,'K');
      chars.add(11,'L');
      chars.add(12,'M');
      chars.add(13,'N');
      chars.add(14,'O');
      chars.add(15,'P');
      chars.add(16,'Q');
      chars.add(17,'R');
      chars.add(18,'S');
      chars.add(19,'T');
      chars.add(10,'U');
      chars.add(21,'V');
      chars.add(22,'W');
      chars.add(23,'X');
      chars.add(24,'Y');
      chars.add(25,'Z');

      a[i] = chars;

I have to print out the linked list where data for 0 is A, 1 is A and B, when I do this above I get an error message illegal position 0. I believe that I need a nested for loop to print this, but I am not sure where to even begin.

  • @Reimeus `Lists` and more concretely `ArrayLists` have variable length... `ArrayList[1]` creates an arrayList array of one element ;) – Oerd Oct 25 '15 at 17:46
  • 1
    youve created an array of size 1 with nothing inside it – Reimeus Oct 25 '15 at 17:47

1 Answers1

0

Judging by the name of the makeLists method, you're probably expected to create the List here. So within your for loop, you possibly need something like:

a[i] = new ArrayList();

... possibly populating the list with actual items, as required?

Do note here you're dealing with an Array of ArrayLists. As is, you have a sized but otherwise "empty" Array, containing no ArrayLists.

ziesemer
  • 27,712
  • 8
  • 86
  • 94