Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

error with printing ArrayList elements

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic