• Post Reply Bookmark Topic Watch Topic
  • New Topic

I've created an array list. How do I iterate through it to read the values?  RSS feed

 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my class OnPlanUserModule I have created an array list. I would like to know how to iterate through the list to read the values of each element.

Thank you.


\
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way is with an enhanced for() loop.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tere are several ways to do this. For example, one way ito iterate through the entire list is

Well, since Carey shows the same way, I'll show another:

This method can be useful if you need to know which entry i you are dealing with.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could make a Stream out of the List and process it that way:



That way if you need to filter the list, or sort it, or any of many other things you can do with a Stream, it's straightforward.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

As List interface implements forEach method you don't need to create a stream. This could be simplified to:
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:. . . List interface implements forEach method . . .
Good point; the method appears to come from a superinterface of List. Details here.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where the solution says 'do something with e here.' I wasn't able to actually figure out how to access the list

for example, to print firstname()

I came up with
System.out.println("firstName() "+i+list.get(i)[0]);

error was 'Array required but Employee found.

So what is proper syntax/
 



 
Stefano Carniel
Greenhorn
Posts: 27
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a suggestion: change the name of your getters and setters to be clearer (getFirstname(), setFirstname(String firstname),...).

then, to print the firstname:


In your code you're applying the indexing to an object of type Employee, which doesn't make sense.

Most elegant solution, for Java >= 8
 
Dave Tolls
Ranch Foreman
Posts: 3064
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Greenbaum wrote:where the solution says 'do something with e here.' I wasn't able to actually figure out how to access the list


You don't need to access the List.
You already have 'e', which is the current Employee you are working with from the List.

so e.getFirstName() (or whatever method name you use) would return the firstname.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response.
I changed getters and setters

Tried first approach but got error message

ant -f F:\\Users\\User\\Documents\\NetBeansProjects\\OnPlanUserModule -Dnb.internal.action.name=build jar
init:
Deleting: F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\built-jar.properties
deps-jar:
Updating property file: F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\built-jar.properties
Compiling 1 source file to F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\classes
F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\src\onplanusermodule\OnPlanUserModule.java:43: error: array required, but ArrayList<Employee> found
             System.out.println("firstName() "+i+list[i].getfirstName());
1 error
F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\nbproject\build-impl.xml:930: The following error occurred while executing this line:
F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)




 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\src\onplanusermodule\OnPlanUserModule.java:43: error: array required, but ArrayList<Employee> found
             System.out.println("firstName() "+i+list[i].getfirstName());
1 error 

Can you post the code so that the line number shown in the error message agrees with the line numbers in the posted code?

array required, but ArrayList<Employee> found

The []s used in the code are for accessing elements in arrays.  List objects use the get() method to access its contents.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry
ant -f F:\\Users\\User\\Documents\\NetBeansProjects\\OnPlanUserModule -Dnb.internal.action.name=rebuild clean jar
init:
deps-clean:
Updating property file: F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\built-clean.properties
Deleting directory F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build
clean:
init:
deps-jar:
Created dir: F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build
Updating property file: F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\built-jar.properties
Created dir: F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\classes
Created dir: F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\empty
Created dir: F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\generated-sources\ap-source-output
Compiling 3 source files to F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\build\classes
F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\src\onplanusermodule\OnPlanUserModule.java:45: error: array required, but ArrayList<Employee> found
             System.out.println("firstName() "+i+list[i].getfirstName());
1 error
F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\nbproject\build-impl.xml:930: The following error occurred while executing this line:
F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)


 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
F:\Users\User\Documents\NetBeansProjects\OnPlanUserModule\src\onplanusermodule\OnPlanUserModule.java:45: error: array required, but ArrayList<Employee> found
             System.out.println("firstName() "+i+list[i].getfirstName());
1 error


array required, but ArrayList<Employee> found

The []s used in the code are for accessing elements in arrays.  List objects use the get() method to access its contents.

Remove the []s and use the get method to access the contents of the ArrayList.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
array required, but ArrayList<Employee> found

The []s used in the code are for accessing elements in arrays.  List objects use the get() method to access its contents.

In case you missed it, Norm has the solution for you here.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did that and got following error
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at onplanusermodule.OnPlanUserModule.main(OnPlanUserModule.java:45)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at onplanusermodule.OnPlanUserModule.main(OnPlanUserModule.java:45) 

That error message does not say what the compiler's error was.  Can you copy the error from the compiler and paste it here?
You need to know how to find compiler errors so you can fix them.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey
get(i) did it!
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have got that bit solved, but I can see some more problems with your Employee class.
The method naming is very iffy; using lastName as an overloaded method for getting and setting a field looks dubious to me.
Another thing is, because you don't demand those details as constructor parameters, you might allow Employee objects to exist in an inconsistent state, with pieces missing.

I also think you would do well to provide your class with a toString method.

Don't use Strings to represent dates; use local date or similar. Why have you got a finishing date field in an employee? Surely, when you have a finishing date, the employee ceases to be an employee and is an ex‑employee.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!