-3

Learning how to use Java ArrayLists it keeps throwing the following exception:

496cc7/packlist.java:5: error: cannot find symbol List alist = new
ArrayList<>(); ^ symbol: class List location: class packlist 1 error

This is entirely new to me so im not sure what is actually wrong any help would be greatly appreciated

import java.util.ArrayList;
import java.util.Collections;
class packlist {
  public static void main(String args[]) {
    List <String> alist = new ArrayList<>();
    alist.add("Mark");
    alist.add("William");
    alist.add("John");
    alist.add("Dave");
    alist.add("James");
    System.out.println("The items of alist are: " + alist);
    Collections.reverse(alist);
    System.out.println("The reversed items of alist are: " + alist);
  }
}

the expected result is the contents of the ArrayList, but in reverse i am getting the error above relating to line 5

user11222393
  • 3,245
  • 3
  • 13
  • 23
Akio Kun
  • 1
  • 1
  • You're importing Collection but not List. You may have mean Collection list = ... or perhaps forgot to add import java.util.List; _READ_ the error message - while they're not always clear especially when initially learning java - it is telling you what's at issue here. The java compiler isn't aware of what the List class is. This implies that it's either mispelled or there is a missing import. – Craig Taylor Oct 23 '19 at 14:39
  • 2
    Possible duplicate of [What does a "Cannot find symbol" compilation error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – Tom Oct 23 '19 at 14:45
  • thanks for the clarification dude! ive been noticing a trend with the exercises im being given to complete where the first exercise is given to me as a code block that is relatively simple but the optional ones are covering things i haven't yet been taught (this optional exercise appeared right after a brief introduction to ArrayList) – Akio Kun Oct 23 '19 at 14:46

1 Answers1

1

Add

import java.util.List;

and write

List<String> alist = new ArrayList<>();
//  ^
//  `-- no blank here

If you don't want to import List, use

ArrayList<String> alist = new ArrayList<>();
  • the requirements of the assesment is to use ArrayList and Collections explicitly – Akio Kun Oct 23 '19 at 14:32
  • im guessing the course provider didn't think to specify that i would need 'import java.util.List' as this is an optional exercise and asks for use of ArrayList and Collections only ah well importing java.util.List fixed it (i find it odd that on the first part of the exercise it didn;t throw a single error regarding this) – Akio Kun Oct 23 '19 at 14:35