Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to define a List<ofSomeType>  RSS feed

 
Jorge Maldonado
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a structure like this:

* ID
* Last Name
* First Name
* Grade

Is it possible to define a List<> of such a type/structure? If so, can you please tell me how to add/remove/get elements or give a reference so I can read about it?
I have not found a good source for reading.

I will very much appreciate your feedback.
 
Stephan van Hulst
Saloon Keeper
Posts: 7804
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, you should make a class of it, and then declare a variable as something like List<Student>.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can google the Java API for ArrayList, or other List type, and it will have what you need.

All you have to do is define your object and then make the ArrayList of that type:
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You create a class with those fields. Then you can create a list like this

Of course there are other list types in addition to ArrayList.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java® supports lots of classes and interfaces for Collections; you can read about them in the Java™ Tutorials. You may not need to read the whole of that section at once. I am surprised you have not found anything to read; I would have thought most books would cover Collections. Lists are probably the most commonly used kind of Collection.Note that some collections methods require the elements to override the equals() and hashCode() methods correctly.
 
Jorge Maldonado
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your responses. I am a Java beginner and I am having a hard time with this issue, but I guess I finally made it. I only have one last question.
The following is a simple class:



And here is the main program:



The output is correct because I get:
1 Elthon John
2 Barry White
3 Frank Zappa

As you can see, I instantiated "myClass" 3 times to insert 3 items in the ArrayList. Is there a better way to do this so "myClass" is instantiated only once?

Respectfully,
Jorge Maldonado
 
Jorge Maldonado
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found the answer. All I need to do is to instantiate the class using the same object name, this way I get a new reference:

 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Maldonado wrote:. . . I instantiated "myClass" 3 times to insert 3 items in the ArrayList. Is there a better way to do this so "myClass" is instantiated only once? . . .
No.

The idea of object programming is that you should create one object to represent one instance in real life. You would not expect Frank Zappa to be the sam person as Elton John. Nor would you expect the frankZappa object to be the same object as the eltonJohn object. You can see what would go wrong like this:-What I have done is reuse the same object twice, so one object is added thrice to your List. The changes are in lines 15 and 21. I have also shortened your line 28 by breaking it into three lines for reasons explained here. That last change will not alter your functionality at all.

Now run that program and see what happens

While you do that, I shall have things to say about your myClass class.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe nobody has told you, but there are conventions about names. Class names should start with CapitalLetters and variable names should start with lowerCaseLetters. Please use id not ID and MyClass not myClass. Actually that should be something like Singer, because it should always be obvious to everybody reading your code what the names mean.
You should give all the fields in your classes private access. It is wrong for other code to change the values of your objects. That means you need a constructor, too. I would suggest you write a toString method: a possible example follows:-The @Override annotation will make the compiler notice if you have any spelling errors e.g. tostring. Try tostring and see what happens. There are other ways to create a nice String but for the time being let's stick to the nice simple + operator. Now you have the toString method, you can simply print the object. Your line 28 becomes:-
System.out.println(list.get(i));
Now isn't that better
 
Stephan van Hulst
Saloon Keeper
Posts: 7804
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing, you can iterate through collections using the advanced for-loop. Combining that with Campbell's advice gives you this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!