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 5 of 5

Thread: Storing an array into an array

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Storing an array into an array

    I have an array of customers, and I want to keep track of the DVD titles each customer borrowed, so I am guessing that I will create an array for each customer. Can someone help me set it up or explain how I can execute this. This is what I have so far:


    public class Customer
    {
         public Customer(String name, String number){ }
    }
     
     
    public class CustomerDatabase
    {
         Customer[] theCustomer = new Customer[100];
     
         public void addCustomer{ }
         ...
         ......
         ........
    }

    Suggestions/help would be appreciated. Thanks.


  2. #2
    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: Storing an array into an array

    For database of this type, I'd strongly suggest implementing a Hash Table. The reason is I'm guessing you want to look up a certain person, and to do that an array would have to look through every element in the array, where as a hash table more-or-less will get you O(1) performance (depending on how many collisions there are, and what your hash function is).

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    vluong (September 22nd, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Storing an array into an array

    Thanks for the suggestion. I actually haven't learned hash codes, yet, so I'm still trying to figure this out. I understand that my program won't be very efficient, but right now, I just want to focus on completing the assignment.

  5. #4
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: Storing an array into an array

    In your CustomerDatabase you have an array of Customer objects.
    Each Customer object can have an array of Dvds it have borrowed.
    So all you need to do is to add an array of dvds to your Customer class (you already have name and number there)
    So it would look something like this. However if you can I would suggest using a list instead of array.
    public class Customer {
     
    	private String name;
    	private long id;
    	private Dvd[] dvdList;
     
    	public Customer(String name, long id){
    		this.name = name;
    		this.id = id;
    	}
     
    	public void loan(Dvd dvd){
    		//add dvd to dvdList
    	}
     
    	public void deliver(Dvd dvd){
    		//remove dvd from dvdList
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	public long getId() {
    		return id;
    	}
     
    	public void setId(long id) {
    		this.id = id;
    	}
     
    }

    Oh and for that you will need a Dvd class, unless you want to store pure strings..
    Dvd class would look something like this:

    public class Dvd {
     
    	private String name;
    	private long id;
     
    	public Dvd(String name, long id){
    		this.name = name;
    		this.id = id;
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
     
    	public long getId() {
    		return id;
    	}
     
    	public void setId(long id) {
    		this.id = id;
    	}
    }
    Last edited by Dalisra; September 22nd, 2009 at 06:36 AM.

  6. The Following 2 Users Say Thank You to Dalisra For This Useful Post:

    helloworld922 (September 22nd, 2009), vluong (September 22nd, 2009)

  7. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Storing an array into an array

    Will try that! I have something very similar set up, but I will make some changes. Thanks.

Similar Threads

  1. Reading .txt and storing into an array
    By vluong in forum Collections and Generics
    Replies: 1
    Last Post: January 4th, 2010, 02:07 PM
  2. All possible combinations of a 1D array
    By Gabriel in forum Collections and Generics
    Replies: 2
    Last Post: August 11th, 2009, 10:35 PM
  3. [SOLVED] I am trying to shuffle up an array
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: July 18th, 2009, 03:31 PM
  4. Program of an Array in Java
    By eric_kapre in forum Collections and Generics
    Replies: 1
    Last Post: February 5th, 2009, 06:30 AM