0

I'm trying to create an array list of objects. I am a student and my professor requires that all declaration are before the executable code and that all instantiations or initializations (not sure which term) are done in the executable code. I'm new to the List or ArrayList concept and i can't quite figure out how to get this started.

List <Room> roomAry;    //declare array object for rooms

//initialize room array
roomAry = new List<Room>();

This keeps telling me it can't compile.

The error follows:

List is abstract; cannot be instantiated roomAry = new List ();

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
wizeman02
  • 55
  • 6

2 Answers2

0

Because it should be roomAry = new ArrayList<Room> (). It's correct for both .NET and JAVA. List is just an interface (JAVA)

nikis
  • 11,166
  • 2
  • 35
  • 45
0

The error is telling you exactly what the problem is. The List is an interface and is thus abstract. You cannot create an instance of an abstract class or an interface; you always have to create an instance of a concrete class.

Please see the All Known Implementing Classes: section under the List interface in the javadocs. (Note that if the implementing class itself is abstract, then you cannot use it)

So you can use one of the implementing classes for the instantiation. One of the simpler ones is ArrayList.

roomAry = new ArrayList<Room>();
Wes
  • 894
  • 1
  • 7
  • 17