• Post Reply Bookmark Topic Watch Topic
  • New Topic

iterate over a list and get values from a object?  RSS feed

 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first i looked at this example and understand this fine:



BUT if I have a WeekdayClass and have make 3 object's
Weekday1,Weekday2,Weekday3..
and I have a string called Name, and i have make setName and done:
Weekday1.setName("Monday");
Weekday2.setName("Tuesday");
Weekday3.setName("Wednesday");

ant ofcorse I have a getName...

NOW
IF I would like to do the same ting how do I do this?


I just get stuck when i try this.. :



I hope you understand my problem :-)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shouldn't this:


be this:


etc...

just a note...Putting "???" doesn't really tell us what problem you are having. You may want to read our TellTheDetails FAQ.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isn't the iterator that is returned from an ArrayList<Weekday>, an Iterator<Weekday>? And not a Iterator<String>?

Henry
 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, can you please explain.

do i have to make this:

and then:


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isn't the element that is returned from an Iterator<Weekday>, an Weekday object? And not a String object?

Henry
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and then:
 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
and then:


INTERESTING!!
so the System.out.println(element); prints for example wednesday?
and the System.out.println(element.getName()); prints "only" : "Weekday3" .

so:
is there anyway to get the position of the iterator?

so if i want to delete (put it to null) for example the object in arraylist pos 2? or so?
or set this object to another dayname..
like something like:
(FYI:"arraylist" name is list)

list["iterator pos" ].setname("Sunday");
and
list["iterator pos" ] = null;

or something....


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niklas Karlsson wrote:
is there anyway to get the position of the iterator?


What should the iterator return if it is an iterator for a collection that does not have a concept of a position?

Niklas Karlsson wrote:
so if i want to delete (put it to null) for example the object in arraylist pos 2? or so?


The iterator has a method to allow you to delete the element. Deleting the element, in any other way, while iterating can yield a concurrent modification exception.


But ... to elaborate your question... if you absolutely must traverse the collection by index number, then just do so -- you don't actually need to use the iterator.

Henry
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niklas Karlsson wrote:so: is there anyway to get the position of the iterator?

 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:


The iterator has a method to allow you to delete the element. Deleting the element, in any other way, while iterating can yield a concurrent modification exception.


But ... to elaborate your question... if you absolutely must traverse the collection by index number, then just do so -- you don't actually need to use the iterator.

Henry


so i did not know i could loop a arraylist without a iterator:-)

like
for(i=0, i<list.getlength() i++){
sytemoutprintln ("pos: " + i + " name is: " + list[i].getName) ); //for example "wednesday"
]


 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niklas Karlsson wrote:so i did not know i could loop a arraylist without a iterator:-)

like
for(i=0, i<list.getlength() i++){
sytemoutprintln ("pos: " + i + " name is: " + list[i].getName) ); //for example "wednesday"
]

Right idea, wrong syntax. See my example.
 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EXELENT!
here are my working test if someone have the same problem,
not a nice way put this is only for some test, and NOT a "convention" way..






 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!