aspose file tools*
The moose likes Beginning Java and the fly likes Use of generic classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use of generic classes" Watch "Use of generic classes" New topic
Author

Use of generic classes

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Ok, I understand the use of generics to simplify code that you can have one method that takes in a multiple of different types and may return different types as well. How do I use ArrayList<Patient> to hold them in. Is it like calling a method from another class?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9995
    
    7

Dustin Schreader wrote:Ok, I understand the use of generics to simplify code that you can have one method that takes in a multiple of different types and may return different types as well.

I am not really sure I understand what you are saying here.
Generics is designed for type safety. You define a collection to accept a particular type of object. If you try to add another type, you would get a compile error.

Dustin Schreader wrote:How do I use ArrayList<Patient> to hold them in. Is it like calling a method from another class

It depends on how your code is structured. Ideally, following the encapsulation principles and good programming practice, you would never expose your list directly for other classes to manipulate. You would provide getter/setter methods to manipulate the list objects.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Maneesh Godbole wrote:
Dustin Schreader wrote:Ok, I understand the use of generics to simplify code that you can have one method that takes in a multiple of different types and may return different types as well.

I am not really sure I understand what you are saying here.
Generics is designed for type safety. You define a collection to accept a particular type of object. If you try to add another type, you would get a compile error.


I am not completely sure either, but this sound like the description for generic methods.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
This thread is more in response to the help I was given yesterday. I did a little reading and understood a little about generics but I'm still a little fuzzy on what winston was getting at. http://www.coderanch.com/t/560672/java/java/problems-creating-new-larger-array#2545398
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
Also, I've been trying to look up more on "this" I learned it a while ago in class and I'm still confused as to how I'd use it but Winston gave it in this example.

is this.code referring to the private int code; at the top of the class that that it can differentiate between the parameters of the method patient and the class variables? And if that's correct is that the only reason this. is used, to differentiate between variables?
Sagar Dabas
Ranch Hand

Joined: Nov 15, 2011
Posts: 47

I have gone through same scenario few days back.....this what i did

1. Created classes like your "Patient" class (but i had more than one).
2. Stored the classes objects in the ArrayList--->(ex : patientList.add(new Patient()).
3. For saving the instance variables of the classes i serialized the arraylist. (saved 300 lines of code as i had lots of instance variables in a class.)
4. then you just need to deserialize the arraylist into an arraylist for reading back the files, and you can refer to the instance variables using objects in your arraylist.

if you follow these steps then for reading, deleting, editing and creating new file you would need only 40 to 60 lines of code.
and also if you want to store another class than Patient , than you don't even have to write the separate code for that class (that's polymorphism).

"this." refers to the current object's instance variable and the one in the parameter list is the local variable of the function. Since both variables have same name , local variable hides the instance variable, so to make a difference yes we use this keyword.


Live Curious!!!
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 102
So since I had a an array of arrays previously and now I want to use an arrayList it seems easy enough to add more and more elements to an ArrayList but how do I sort through the list like i did when I was using a for loop to sort through the array of arrays? It seems like I'd be sorting through each entire string of combined variables created by my patient class when, let's say I just wanted to compare against the code of the pt to do a search on them not the code, name, physician, floor, and time.
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137

The simplest thing to do here is to override equals() in the Patient class and use List.indexOf(Object). If my memory serves me correctly indexOf() invokes the equals() method on each collection member, passing your object as the argument, and returns the index where it finds a match, or -1.

You will want to implement the Comparable interface. This sort of thing will get a lot easier once you do that.

However, if Patient is final or you need more flexibility on sorting and comparing Patients you will need to read up on the Comparator interface. Bates & Sierra's certification exam guide has a really good explanation of that.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Use of generic classes
 
Similar Threads
Why Overloading..?
Generics and interface inheretence
returning multiple types
generics in servlets(sessions)
Overwriting and Overloading Question