Results 1 to 11 of 11
- 08-23-2011, 12:34 PM #1
Member
- Join Date
- May 2011
- Posts
- 97
- Rep Power
- 0
How to declare array of Unknown Length for below code
Hi Guys,
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?Java Code:String [] DependentView = new String[1000]; String [] ParentView = new String[1000];
Sandeep
- 08-23-2011, 02:04 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,420
- Blog Entries
- 7
- Rep Power
- 17
Yep, use a List<String> of some sort.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-23-2011, 02:34 PM #3
The length of one String object won't be any problem when assigning it to an element of an array.i want to store String of unknown length
The number of Strings that you want to put in the array would be restricted to the size of the array.
- 08-23-2011, 07:56 PM #4
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.
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.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();
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.
- 08-23-2011, 08:04 PM #5
If you use Generics the compiler will take care of the conversion/casting for you.Remember that each element in an ArrayList is an Object so you will need to convert each element to a String.
The ArrayList class has a method that will create and fill an array for you.there might be another more efficient way to do the same thing.
- 08-23-2011, 09:25 PM #6
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-23-2011, 09:27 PM #7
The toArray method is overloaded. Look at the next one, it will return a String array.
Java Code:public <T> T[] toArray(T[] a)
- 08-23-2011, 10:02 PM #8
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.
- 08-24-2011, 11:42 AM #9
Member
- Join Date
- May 2011
- Posts
- 97
- Rep Power
- 0
Hey later how can i flush values When i am using String[]
Sandeep
- 08-24-2011, 11:50 AM #10
Member
- Join Date
- May 2011
- Posts
- 97
- Rep Power
- 0
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
- 08-24-2011, 01:31 PM #11
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
-
How to Declare and Initialize static array
By parulmahajan in forum New To JavaReplies: 6Last Post: 08-05-2011, 07:48 AM -
Trying to display the length of the array using tags
By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-03-2010, 12:15 AM -
Declearing array of unknown size
By new2java2009 in forum New To JavaReplies: 9Last Post: 04-16-2010, 02:35 AM -
java Array.length property
By X-ion in forum New To JavaReplies: 0Last Post: 04-07-2010, 08:45 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks