Results 1 to 10 of 10
  1. #1
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default Declearing array of unknown size

    How do I declear an array of unknow size? Can I do the following where getNames() will return an array of Strings? Thanks!

    String[] names = new String[0];
    names = getNames();

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    You can either set the array length to a large number or use an ArrayList instead. Arrays have a fixed length so if you don't know the size when you're creating it there's no way to change it unless you transfer it to another array.

    Using ArrayList is easier because you can just append a new item to the end and the length can increase as needed.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Using ArrayList is easier because you can just append a new item to the end and the length can increase as needed.
    Easy as well as most safe way to handle dynamic changes. As xcallmejudasx says use of Arrays, if you have to change the size, so that copy content from one array to another with dynamic size changes. That's not safe.

  4. #4
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    I have to use array because the method getName() returns an array of string. Will the way I mentioned above works?

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Why even initialize it? Why don't you just do something like this
    Java Code:
    String[] names = getNames();

  6. #6
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    The reason I have to init it first is because the way I am using. If I do "String[] names = getNames();", the scope of "names" will not allow me to have the "return statement" at the end of the method.

    String [] names = new String[0];

    try {
    ....
    names = getNames();
    ...
    } catch (Throwable e)
    ...
    }

    return names;

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    The problem with initializing to a default value is it sounds like you are never going to use it and even if you do, its only because getNames() errored. You should still have the scope for names inside the method, so a return is possible.

    Sounds like you should do this then....


    Java Code:
    String[] names = null;
    try {
         names = getNames();
    } catch (Throwable e) {
         e.printStackTrace();
    }
    if (names != null) {
         ....//Processing or doing whatever
    }
    return names;
    Last edited by StormyWaters; 04-15-2010 at 06:11 PM.

  8. #8
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Posts
    60
    Rep Power
    0

    Default

    Great! Thanks! I will try it out.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by new2java2009 View Post
    I have to use array because the method getName() returns an array of string. Will the way I mentioned above works?
    Cannot change in into ArrayList? That's the most safest way to handle those situations.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by new2java2009 View Post
    Great! Thanks! I will try it out.
    Yeah, that's a nice way to handle your requirment. And also you can handle any dynamic array sizes. Give a try and see.

Similar Threads

  1. Changing size of Array
    By ravian in forum New To Java
    Replies: 3
    Last Post: 06-05-2012, 08:17 PM
  2. Replies: 22
    Last Post: 01-05-2009, 01:03 AM
  3. Doubling the size of an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:42 PM
  4. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 10:37 PM
  5. Maximum size of an array
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-20-2007, 11:11 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
  •