Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Array object and constructors

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Array object and constructors

    This is how you make a ArrayList
    ArrayList alist = new ArrayList(3);
    alist.add("something");
    alist.add("something2");
    alist.add("something3");

    I dont quite understand you question.

    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?

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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
    // 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);
    	}
    }
    //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

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array object and constructors

    Plz help me...........

  5. #5
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Array object and constructors

    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.

  6. The Following User Says Thank You to Bryan For This Useful Post:

    TarunN (May 4th, 2010)

  7. #6
    Junior Member
    Join Date
    Apr 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

    Regards,
    Tarun

  8. #7
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Array object and constructors

    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:
    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.

  9. #8
    Junior Member
    Join Date
    Apr 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    // inside the Officer class
    // should fix your problem
    public String toString()
    {
         return "Name: " + name + " code: " + code;
    }

  11. #10
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Array object and constructors

    Quote Originally Posted by Bryan View Post
    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.

  12. #11
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Array object and constructors

    Quote Originally Posted by dlorde View Post
    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.

  13. #12
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Array object and constructors

    It's all documented in the ArrayList API docs...

  14. #13
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default 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

  15. #14
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Array object and constructors

    Quote Originally Posted by Bryan View Post
    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

  16. #15
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Array object and constructors

    Quote Originally Posted by dlorde View Post
    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
    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.

Similar Threads

  1. Help with object
    By Spangle1187 in forum Collections and Generics
    Replies: 3
    Last Post: April 22nd, 2010, 05:34 PM
  2. How do you set an object in array to null value?
    By Arius in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 03:50 AM
  3. Object o = new Object(); need help
    By zeeshanmirza in forum Object Oriented Programming
    Replies: 11
    Last Post: January 6th, 2010, 10:01 PM
  4. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM
  5. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 05:09 AM

Tags for this Thread