1

In my continuing education on Arrays and ArrayLists I'm trying to smarten up my code by passing an ArrayList from one method to another. Here is my code:

public void exampleArrayList () {
    ArrayList<String> al = new ArrayList<String>();
    al.add("AZ");
    al.add("BY");
    al.add("CX");
    al.add("DW");
    al.add("EV");
    al.add("FU");
    al.add("GT");

    display(al);
}

public void display(ArrayList al) {

    System.out.println("Index of 'AZ': " + al.indexOf("AZ"));
    System.out.println("Index of 'FU': " + al.indexOf("FU"));
    System.out.println("Index of 'AA': " + al.indexOf("AA"));
    System.out.println("Index of 'CX': " + al.indexOf("CX"));

    // for (String row : al)
    //   System.out.println("Value at Index " + al.indexOf(row) +
    //      " is " + al.get(al.indexOf(row)));

    for(int i = 0; i < al.size(); i++)
        System.out.println("Value at Index " + al.indexOf(i) +
            " is " + al.get(al.indexOf(i)));
}

In the display method works with both for statements commented out. The for statement that is currently commented out doesn't work because row is looking for a String but get's assigned an object even although array al is a string. Do I need to cast al into a string or something? This is not the case when I run the for loop when the loop is in the same method that created the ArrayList in and I don't understand the difference.

The second for statement that isn't commented out causes a crash giving me the following runtime error:

java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1

I tried changing the i < al.size() to a hard coded number and it still failed and I don't know why.

Marko
  • 20,385
  • 13
  • 48
  • 64
Airfix
  • 127
  • 13

3 Answers3

3

1) You need to pass it as an ArrayList<String>:

public void display(ArrayList<String> al) {
                             ^^^^^^^^

2) You're searching for the integers in the list. The list doesn't contain any integers, so indexOf returns -1. Then you call al.get(-1) where -1 is obviously out of bounds. I'm not sure what you meant to do here.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • That's exactly what I needed to know. Passing it as a string. Then I see my glaring error regarding the index because I had been mixing up two different pieces of code in my head. Time to take a break I think. Thanks for the help. Slowly this stuff is beginning to make sense. Airfix. – Airfix Apr 27 '16 at 22:29
1

You are using indexOf(), which, given an int, will search for that int and return its index if the list contains it. As this isn't the case - it is a List<String> - you get index out of bounds because you are trying to retrieve an element at index -1. -1 is returned from indexOf() if the element can't be found, which it can't.

This is why you shouldn't use raw types. Use get() and a List<String> as your parameter (no need to make it specifically ArrayLists):

System.out.println("Value at Index " + i +
    " is " + al.get(i));

and

public void display(ArrayList<String> al) {
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
1

One other thing to "smarten the code" is to not use the specific implementation in the declaration or parameters.

public void exampleArrayList () {
   // use the interface List<T>, not the specific implementation ArrayList<T>
   List<String> al = new ArrayList<String>();

   ...
}

// take the Interface, and give it the type
public void display(List<String> al) {
  ....
}

The functionality will be the same, but it is a better programming approach to program to interfaces rather than implementations.

EDIT: Also, unless you really need the index for some reason, using the enhance for loop may be more appropriate

for (String s : al) {
  //some operation
}
KevinO
  • 4,303
  • 4
  • 27
  • 36
  • If I understand the first comment ArrayList being an implementation of a List you're basically saying that I have more flexibility somehow by not calling the implementation (which I don't understand yet at my basic level of java knowledge). Regarding the enhanced loop, I came from basic knowledge of fortran, C and visual basic basic so this is my forst forte into oo programming and I'm still learning the ropes. If in doubt I fall back on my roots. – Airfix Apr 27 '16 at 22:48
  • 1
    @Airfix, [Programming to interfaces](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) has a number of benefits as that answer indicates. [Another take on it here](http://www.fatagnus.com/program-to-an-interface-not-an-implementation/). Generally, you want to preserve the ability to change the implementation if needed. Today you have an `ArrayList`, but perhaps tomorrow a `LinkedList` is more appropriate. If you have `List` in the parameter, you may adjust the implementation, and the other code is unaware. – KevinO Apr 27 '16 at 23:00