Results 1 to 11 of 11
  1. #1
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default How to declare array of Unknown Length for below code

    Hi Guys,

    Java Code:
    String [] DependentView = new String[1000];
    String [] ParentView = new String[1000];
    This is what i have declared of length 1000, but i want to store String of unknown length for same String statement i have passed, any idea?

    Sandeep

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Yep, use a List<String> of some sort.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default

    i want to store String of unknown length
    The length of one String object won't be any problem when assigning it to an element of an array.
    The number of Strings that you want to put in the array would be restricted to the size of the array.

  4. #4
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Since they are all Strings you could use an ArrayList to first store the Strings. Each time you invoke the .add method the ArrayList size increases by one element. When you're done filling the ArrayList, you can create your String array and size it from the ArrayList size() method.

    Remember that each element in an ArrayList is an Object so you will need to convert each element to a String.

    ex.
    Java Code:
    ArrayList list = new ArrayList();
    
    for( int i = 0; i < 100; i++ )
        list.add( "stuff" );
    
    String[] strArray = new String[ list.size() ];
    
    for( int j = 0; j < strArray.length; j++ )
        strArray[ j ] = list.get( j ).toString();
    For example sake I created an ArrayList and put 100 words in it. I dimensioned the String array to the size of the ArrayList. I then converted each element of the ArrayList to a String and deposited them into the String array.

    Now this will work, there might be another more efficient way to do the same thing.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default

    Remember that each element in an ArrayList is an Object so you will need to convert each element to a String.
    If you use Generics the compiler will take care of the conversion/casting for you.

    there might be another more efficient way to do the same thing.
    The ArrayList class has a method that will create and fill an array for you.

  6. #6
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    If you use Generics the compiler will take care of the conversion/casting for you.


    The ArrayList class has a method that will create and fill an array for you.
    I looked at the ArrayList in the API and the toArray() method returns Object[]. Would the OP not then have to create an array of Strings anyway since Object and String are not the same type?
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default

    The toArray method is overloaded. Look at the next one, it will return a String array.
    Java Code:
    public <T> T[] toArray(T[] a)

  8. #8
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Ok, after some reading:

    Java Code:
    ArrayList<String> list = new ArrayList<String>();
    
    for( int i = 0; i < 100; i++ )
        list.add( "stuff" );
            
    String[] strArray = list.toArray( new String[ list.size() ] );
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  9. #9
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    Hey later how can i flush values When i am using String[]

    Sandeep

  10. #10
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    Actually the problem in for the first loop i have 4 values in my array, later for second loop i have 2 values. So when executing second time my String[] has 3rd and 4th values what that array had during first loop, so how to flush them out or clear them

    Sandeep

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default

    What do you mean by "flush"?
    Do you want to remove them from the array?
    One way would be to set them to null. Then your logic would need to handle the case of the array having null entries.
    Another way is to create a new array with a reduced size and copy the elements you want to keep from the old array to the new array.
    These are reasons for you to consider using an ArrayList. The ArrayList class has methods to remove elements.

Similar Threads

  1. How to Declare and Initialize static array
    By parulmahajan in forum New To Java
    Replies: 6
    Last Post: 08-05-2011, 07:48 AM
  2. Trying to display the length of the array using tags
    By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-03-2010, 12:15 AM
  3. Declearing array of unknown size
    By new2java2009 in forum New To Java
    Replies: 9
    Last Post: 04-16-2010, 02:35 AM
  4. java Array.length property
    By X-ion in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 08:45 AM
  5. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 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
  •