Array object and constructors
Hi all,
I wish to create an array object which can collect three string values…..
Collecting them from keyboard entry and then reprint them in output one by one……
I am able to collect for one time for an object and constructor
But if I convert the object to array the constructor cannot recognizes it……
How can I do the same……
Please help……
Regards,
Tarun:confused:
Re: Array object and constructors
This is how you make a ArrayList
Code :
ArrayList alist = new ArrayList(3);
alist.add("something");
alist.add("something2");
alist.add("something3");
I dont quite understand you question.
Quote:
I am able to collect for one time for an object and constructor
But if I convert the object to array the constructor cannot recognizes it……
What?
Re: Array object and constructors
Thanx bryn for reply..
But I think my question is not clear………..
I am giving you some more detail…..
My constructor
Code :
// class staff
class staff
{
String Code,Name;
staff(String a, String b)
{
String Code= a;
String Name= b;
}
}
//subclass officer extends staff
class officer extends staff
{
String Grade;
officer (String a, String b,String c)
{
super (a,b);
Grade=c;
}
String showData()
{
String n=Code+Name+Grade;
System.out.println(n);
return (n);
}
}
Code :
//main class I removed other subclasses for clearance
import java.io.*;
class employee
{
public static void main(String [] Tarun)
throws IOException
{
System.out.print("Welcome to SAGA \nGive the details for new entry:\nJob Code:\nA)Officer \tB)Teacher\tC)Typist\n");
BufferedReader Job= new BufferedReader (new InputStreamReader (System.in));
String Code=Job.readLine();
System.out.print("Name:");
BufferedReader Nm=new BufferedReader (new InputStreamReader(System.in));
String Name=Nm.readLine();
if (Code.equals("Officer"))
{
System.out.print(Code+Name+"Please Enter Grade:");
BufferedReader Gr=new BufferedReader (new InputStreamReader (System.in));
String Grade=Gr.readLine();
System.out.println(Code+Name+Grade); //just checking that all input are correct.
officer O=new officer(Code,Name,Grade);
String show=O.showData();
}
}
Now if I wish to maintain the input such that each time I enter new officer detail and ask for reprint it should give all the entries for Officer……..
I hope I have made it a bit clear…..
Do reply……
Regards,
Tarun
Re: Array object and constructors
Re: Array object and constructors
Code :
String n=Code+Name+Grade;
This is no good.
You need to understand inheritance. Code and name are need to be called like: super.code, super.name and grade can be called like this.grade.
Re: Array object and constructors
Dear brayn,
Thanx for replying again but the problem is not with the variables my code runs with that (String n=Code + Name + Grade ;).
As per my knowledge, the use of super.code or super.name will be needed if I use the same variables in sub class.
I am having problem with how to define an array which can hold the officer object as its variable….
For example if I define Of1, Of2, Of3…..as officer object. I wish to store them in a array object of offi type such that
Offi [0] = Of1;
Offi [2] = Of2;
Offi [3] = Of3;
One more condition is that i can put n no. of input to the Offi array object.
hop to see you reply back soon :-bd
Regards,
Tarun
Re: Array object and constructors
Code :
ArrayList<Officer> officerlist = new ArrayList<Officer>();
As long you keep the constructor empty, the ArrayList will have no limit of amount of objects it can hold.
Is this what you need?
BTW if you use a hashtable you can set your own key to the hashtable.
Example:
Code :
Hashtable<Officer> officerlist = new Hashtable<Officer>();
officerlist.put(officer.getId(), officer); //officer id as key and officer object as value
Officer someofficer = (Officer)officerlist.get(officer.getId()); //will retrieve the officer object
Difference between ArrayList and Hashtable is that you can determine your own keys wich also can be strings.
Re: Array object and constructors
thanx bryan,
I am able to creat the Array list......but i think i am not using it in the right way....
I have defined as below steps to my previous code but i am not able to get the values back...
can you please resolve...
ArrayList<officer> officerlist = new ArrayList<officer>();
officerlist.add(0,O);
System.out.println(officerlist); // gives me the memory loacation not the contents as expected
seeking your support
regards
Tarun:eek:
Re: Array object and constructors
That is correct, what you want to do is over-ride the toString() method. The default toString() method of your Officer object is to print out the memory location of the object.
Code :
// inside the Officer class
// should fix your problem
public String toString()
{
return "Name: " + name + " code: " + code;
}
Re: Array object and constructors
Quote:
Originally Posted by
Bryan
As long you keep the constructor empty, the ArrayList will have no limit of amount of objects it can hold.
Not exactly. The constructor argument specifies the initial capacity - you can still add as many more items as will fit in memory. Use the initial capacity to improve efficiency when you know roughly how many items you will be using.
Re: Array object and constructors
Quote:
Originally Posted by
dlorde
Not exactly. The constructor argument specifies the initial capacity - you can still add as many more items as will fit in memory. Use the initial capacity to improve efficiency when you know roughly how many items you will be using.
Ahh, didn't knew that :) I though you would have to make the capacity bigger manual, like calling a method.
Re: Array object and constructors
It's all documented in the ArrayList API docs...
Re: Array object and constructors
I know ;) but I didn't really had the chance yet to explore ArrayLists. I just know the base :( I rather use hashtables :)
Re: Array object and constructors
Quote:
Originally Posted by
Bryan
I know ;) but I didn't really had the chance yet to explore ArrayLists. I just know the base :( I rather use hashtables :)
Thing is, ArrayLists and Hashtables are for quite different uses. ArrayLists are just easy-to-use dynamic arrays, whereas Hashtables are keyed collections for mapping values onto unique keys...
I recommend a browse through the Collections Tutorial to get the basics sorted - it doesn't take long, and it pays off big time :cool:
Re: Array object and constructors
Quote:
Originally Posted by
dlorde
Thing is, ArrayLists and Hashtables are for quite different uses. ArrayLists are just easy-to-use dynamic arrays, whereas Hashtables are
keyed collections for mapping values onto unique keys...
I recommend a browse through the
Collections Tutorial to get the basics sorted - it doesn't take long, and it pays off big time :cool:
Great, thats what I need often, also for my current project. When I put objects in the hashtable I want to sign my own key to it, which often will be objectId() or something similar. Thanks for the link though.