Results 1 to 15 of 15
  1. #1
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default adding to an array

    How do you add something to an existing array?

    I want to add on to an array without overwriting everything i already have stored in the array.

    When i go to add to the array i have it overwrites everything i have and looks something like this:

    before adding:----------- After Adding:
    Will Smith--------------- Tiger Woods
    Denzel Washington------ Tiger Woods
    Tom Cruise------------- Tiger Woods
    Bill Gates--------------- Tiger Woods
    Phil Mickelson----------- Tiger Woods


    Here is my code so far:

    public void add(String name, int empID, GregorianCalendar dateOfHire)
    {
    for(int i = 0; i < employees.length; i++){
    employees[i] = new Employee(name, empID, dateOfHire);
    }
    }
    Last edited by mayhewj7; 02-18-2009 at 11:43 PM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    are you aware of what your for-loop does?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    you're using a for loop to add the same person to every spot in the array, so there should be no surprise that you'll overwrite everything with that one name.

    To solve this, you'll have to add the new person to one spot in the array, but before going further, I'll have to ask you how do you know where you want to add the new person? If it were my program, I'd use the more flexible variable-sized ArrayList and just add the new person to the end.

  4. #4
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    I would use an arrayList but we are not allowed to use an arrayList. Just an array. As for knowing where to add it, it the array if full we are suppose to expandCapicty and add it to the end and if the array is not full, find an empty space in the array and add it there

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    so you know how to loop through the array. if you know how to identify an empty index in it, then you're that much closer to being done. and i'm sure you can get some ideas on changing the array size via search engine

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you need to create a new array everytime, copying the old one over and adding the new stuff to the end.
    Java Code:
    Employees newArray = new Employees[ oldArray.length + 1 ]();
    // copy array in a loop.
    // then add new stuff to newArray.
    OR
    Java Code:
    Employees newArray = java.util.Arrays.copyOf(employees, employees.length + 1 );
    // then add new stuff to the end.
    newArray[ newArray.length-1 ] = new Employee(name, empID, dateOfHire);
    Last edited by angryboy; 02-19-2009 at 03:23 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    it's really inconvenient to increase the size of an array by 1 once 20 is hit if you end up adding more than 20. i'd say just increase by 10 whenever necessary, but depending on the project, you might even wanna add more (usually i double the size).

  8. #8
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    thanks to everybody for the advice.

    angryboy: everytime i try to use the code you provided i get and error,

    incompatible types found Employee[] but expected Employee

    any ideas on why?

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    lets see the code
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    Do you think this would work?

    it compiles....

    public void add(String name, int empID, GregorianCalendar dateOfHire)
    {
    for(int i = 0; i < employees.length; i++){
    if(employees[i].getID() == 0){
    employees[i] = new Employee(name, empID, dateOfHire);
    }
    else if(size() == employees.length){
    expandCapacity();
    employees[employees.length + 1] = new Employee(name, empID, dateOfHire);


    }
    }
    }

  11. #11
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    im trying to loop through the array to make sure there are no empty slots to add the employee before i add one to the end of the array

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Do you think this would work?
    Better than asking this: test it and find out. Best of luck.

  13. #13
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default

    i would test it now but for some odd reason i cant get it to read from my file on my laptop. It will read fine from the computer in lab. I dont know why that is. Its really aggrivating to me

  14. #14
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    ur trying to add an employeee right, so you just need these two lines in the add method.
    expandCapacity();
    employees[employees.length + 1] = new Employee(name, empID, dateOfHire);
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mayhewj7 View Post
    i cant get it to read from my file on my laptop.
    Hm, you'd better debug this problem before too long, else you're in for a very long and frustrating night. Where is your file located in relation to your program's class file? How are you trying to read the file -- as a File or as a Resource? Do you know what your current user.dir is? You may wish to find this out by calling something like:
    Java Code:
    System.out.println(System.getProperty("user.dir"));

Similar Threads

  1. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  2. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 PM
  3. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  4. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 PM
  5. Adding graphics to array
    By romina in forum Java 2D
    Replies: 1
    Last Post: 08-01-2007, 01:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •