Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Collection<myClass> ... How to use collections of classes Posted by keeperols on 28 Oct 2012 at 7:45 PM
I am working on making a lot of my utility apps run on linux
(trying to make the switch from windows)

well i have gotten so used to C# and .net that i wanted to find a comparable language that works on other OS's

after examining code examples it looks like java and c# are pretty similar. but I'm having trouble with a few oddities, in this case
Collections

what I am trying to accomplish is:
1) Create a class with a bunch of properties and methods ...
2) Create a collection of the above class
3) Get the value of any property or call methods from any class in the collection

1 & 2 i believe i have correct.
but 3 is where i am stumped

in C# i am used to doing 3 in this manner:

// 1) Class animal
public class animal{
 public String Name;
 public String Type;
 ...

 public animal(String name, String type){
  this.Name = name;
  this.Type = type;
 }
}

// 2) Create a collection of animals
Collection<animal> Animals = new Collection<animal>();

// Assign values
Animals.add(new animal("Bill", "Duck"));
Animals.add(new animal("Fred", "Dog"));
Animals.add(new animal("Jasmine", "Cat"));
Animals.add(new animal("Jannet", "Rhino"));

/*******************************
 * problem is here *************
 *******************************/
// 3) Use the data

String MyPetName = Animals[2].Name;
// C#: assigns the value of the 3rd element in the
// Collection of animals' Name property
// "Jasmine"


how would i do the above in java?
i cannot find a way to access the members of the objects inside the collection in java. I have searched tutorials and i couldn't find anything that made sense to me. none of the examples I found involved collections of classes.

any help would be grand :)

sorry for the long post

Report
Re: Collection<myClass> ... How to use collections of classes Posted by keeperols on 29 Oct 2012 at 8:17 PM
Answered my own question,

or at least found a work around

i just found out that
for( : ){}

is a foreach() loop :) oh joy i was looking for this loop :D

so by doing
for(animal anml : Animals){
 somethingsomthing = anml.Name;
}


and i just zip thru the collection that way

hope this helps anyone else that was confused as well



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.