Page 1 of 3 123 LastLast
Results 1 to 20 of 46
  1. #1
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default increase array size

    hello all.i have just started learning java programming language.so i tr to write some little programmes.

    i have an array of objects,its size is 0;i wanta to add new object in it from keyboard.
    this is the little part of the code:

    private Person[] arr = new Person[0];

    public void addPerson(Person newMan) {
    Person[] arr1 = new Person[arr.length + 1];
    for (int i = 0; i < arr.length; i++) {
    arr1[i] = arr[i];
    }// end for
    arr1[arr1.length - 1] = newMan;
    arr = arr1;
    }// end addPerson

    it does not work well.after i register new person in it and then i want to print array size and members it says that the size is 0.can anyone help me?what can i do if i want to save the array objects in it
    Last edited by giorgi; 04-07-2011 at 02:26 PM.

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Arrays are static length structures. They stay at whatever size you initialise them to be. Creating an array of length 0 is pointless if you want to insert items.

    So there are two issues - always initialise your array to be the number of items it will contain, and only use arrays for fixed, static numbers of items (e.g. months of the year, cards in a pack, etc). If you want to increase the capacity of an array, you have to create a new one that's bigger, and copy all the items across.

    The solution for dynamically sizeable arrays is to use the ArrayList class, which does all this for you.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Quote Originally Posted by dlorde View Post
    If you want to increase the capacity of an array, you have to create a new one that's bigger, and copy all the items across.
    That's what he's doing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    as you see i made a new arr1 bigger than arr.and at last i refer to arr1 by arr:
    arr=arr1

    so whats the problem

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Quote Originally Posted by giorgi View Post
    as you see i made a new arr1 bigger than arr.and at last i refer to arr1 by arr:
    arr=arr1

    so whats the problem
    I'm not sure. If you want help with code, you'll have to provide an SSCCE that demonstrates what you're actually doing. I don't see anything obviously wrong with what you've posted.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    if you want i can post the whole code and you check it

  7. #7
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    Here's one problem. Unless you populate arr some other way, you will never get inside this loop:
    Java Code:
    for (int i = 0; i < arr.length; i++) {
    //not getting in here
    arr1[i] = arr[i];
    }// end for

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Quote Originally Posted by FlipPoker@gmail.com View Post
    Here's one problem. Unless you populate arr some other way, you will never get inside this loop:
    Java Code:
    for (int i = 0; i < arr.length; i++) {
    //not getting in here
    arr1[i] = arr[i];
    }// end for
    Yeah, and that's what should happen the first time around, since there would be nothing to copy to the new array. That's why I asked the OP for an SSCCE- this code should work (unless I'm missing something), it just depends on how it's being used.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    so what is the right way?

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Quote Originally Posted by giorgi View Post
    if you want i can post the whole code and you check it
    Please don't, unless it is in fact an SSCCE. We simply don't have time to wade through full programs. Narrow your problem down to as few lines as possible- some code that calls this method, then prints something out that you wouldn't expect. You might want to use String instead of Person so you don't have to include that class in the SSCCE.

    The first 'S' stands for "short".
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Quote Originally Posted by giorgi View Post
    so what is the right way?
    The right way is to post that SSCCE.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    public class Person {
    private String name;
    private int groupNo;

    public int getGroupNo(){
    return groupNo;
    }
    public void setGroupNo(int groupNo){
    this.groupNo=groupNo;
    }

    public String getName(){
    return name;
    }
    public void setName(String name){
    this.name=name;
    }

    public Person(){}
    public Person(String name, int groupNo) {
    super();
    this.name = name;
    this.groupNo = groupNo;
    }



    }


    public class PersonContainer {

    private Person[] arr = new Person[0];
    public int getLength() {
    return arr.length;
    }

    public void addPerson(Person newMan) {
    Person[] arr1 = new Person[arr.length + 1];
    for (int i = 0; i < arr.length; i++) {
    arr1[i] = arr[i];
    }// end for
    arr1[arr1.length - 1] = newMan;
    arr = arr1;
    }// end addPerson

    public void register(String name, int groupNo) {
    Person pers = new Person();
    pers.setName(name);
    pers.setGroupNo(groupNo);
    addPerson(pers);
    System.out.println("registracia dasrulda");
    }//end register

    public void printArr() {
    if (arr.length == 0) {
    System.out.println("empty");

    } else {
    for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i].getName() + " "+ arr[i].getGroupNo());

    }// end if
    }// end for
    }// end print

    }


    import java.util.*;

    public class test {
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    while (true) {
    String input = scan.nextLine();

    PersonContainer per = new PersonContainer();
    if (input.equalsIgnoreCase("exit")) {
    System.out.println("Good Bye");
    break;

    } else if (input.equalsIgnoreCase("register")) {
    System.out.println("add person name and group No");
    String a = scan.nextLine();
    int b = Integer.parseInt(scan.nextLine());
    per.register(a, b);
    System.out.println("there is "+per.getLength()+" person");

    } else if (input.equalsIgnoreCase("print")) {

    System.out.println("name / Group No");
    per.printArr();

    } else if (input.equalsIgnoreCase("name search")) {

    }
    else if (input.equalsIgnoreCase("index search")){

    }
    }// end while
    }
    }
    Last edited by giorgi; 04-07-2011 at 02:57 PM.

  13. #13
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    that is my code.so when i write "register" it works and says there is 1 person.when i at to register a new person and write "register" it says again there is 1 person.and when i write"print" it says "empty"

  14. #14
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    sorry i read not to post after i posted

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Sorry, but that's not an SSCCE. First of all, it should be self-contained- meaning it should fit into one file, that way we can just copy and paste it.

    This might seem like nit-picking, but we don't always have time to wade through that much code. I personally only do this while my code is compiling at work- so I only get a couple minutes at a time.

    Also, don't forget the code tags.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    Ok. I see that now. As is, the code appears to work. I think something else is going on.

  17. #17
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    whats going on?i try to fix it for 3 days already.i want arr to save the registered member and register new one
    Last edited by giorgi; 04-07-2011 at 03:12 PM.

  18. #18
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Quote Originally Posted by giorgi View Post
    whats going on?i try to fix it for 3 days already.i want arr to save the registered member and register new one
    Like I said, we can't really help you until you narrow your problem down to an SSCCE. It shouldn't take long, and you'll probably figure it out yourself in the process.

    Somebody else might have time to wade through your code. I don't. Sorry.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by KevinWorkman View Post
    That's what he's doing.
    No, he's starting with an array of size 0, so there can never be any items to copy:
    Java Code:
    private Person[] arr = new Person[0];
    What is that going to achieve?

  20. #20
    giorgi is offline Member
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    thats right,at first the size is 0 and nothing will be copied into the new array,i don't see any problem.then i refer arr1 by arr

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 01-05-2014, 09:12 PM
  2. increase pixel size
    By rosh72851 in forum New To Java
    Replies: 9
    Last Post: 07-31-2012, 07:30 PM
  3. Replies: 4
    Last Post: 01-18-2011, 08:22 AM
  4. Replies: 3
    Last Post: 09-27-2008, 04:46 AM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 07:22 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
  •