• Post Reply Bookmark Topic Watch Topic
  • New Topic

error with printing ArrayList elements  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am getting following error at command line
C:\Users\nirjari\Desktop\javasourcefiles>javac LineCount.java
LineCount.java:18: array required, but java.util.List<java.lang.String> found
System.out.println(lines[i]);
^
1 error

When I am using "lines.length" instead of "lines.size()" to get number of elements in arrayList lines, I am getting following errors
C:\Users\nirjari\Desktop\javasourcefiles>javac LineCount.java
LineCount.java:17: cannot find symbol
symbol : variable length
location: interface java.util.List<java.lang.String>
for(i=0; i<=lines.length; i++) {
^
LineCount.java:18: array required, but java.util.List<java.lang.String> found
System.out.println(lines[i]);
^
2 errors

Why am I getting these errors ? What needs to be fixed ?
I checked sample codes online and they have used "lines.length" and not "lines.size()". So why is lines.length is not working in here ?

Thanks
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you are using a List- you need to use its get(int index) method to retrieve the elements. You would use lines[i] if lines were an array, but lines is a List so you should be using lines.get(i)

And also length is not a part of the List class. You can check out the documentation for the methods supported by java.util.List.
 
Anupam Jain
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Nirjari,

First thing here that you need to look at and understand is that...

Arraylist is not an Array (Yeah... I know they sound similar, but that's the way they liked it.)

And since they are not same... you can't use lines.length or lines[i] notations, which are spcecific to the Array type.

nirjari patel wrote:
When I am using "lines.length" instead of "lines.size()" to get number of elements in arrayList lines, I am getting following errors
C:\Users\nirjari\Desktop\javasourcefiles>javac LineCount.java
LineCount.java:17: cannot find symbol
symbol : variable length
location: interface java.util.List<java.lang.String>
for(i=0; i<=lines.length; i++) {
^
LineCount.java:18: array required, but java.util.List<java.lang.String> found
System.out.println(lines[i]);
^
2 errors

Why am I getting these errors ? What needs to be fixed ?
I checked sample codes online and they have used "lines.length" and not "lines.size()". So why is lines.length is not working in here ?

Thanks


Also, the code that you refferred online must be either using Array type or (otherwise) would be wrong themselves.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anupam Jain wrote:. . . Arraylist is not an Array (Yeah... I know they sound similar, but that's the way they liked it.) . . .
ArrayList is called ArrayList because it uses an array to store its data.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!