• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of generic classes

 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dustin Schreader
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic