Forums Register Login

ARRAY QUESTION

+Pie Number of slices to send: Send
If I have an array <someArray[]> that has already been initialized at [100]. Will the code

give me the entire contents of an array or just the parts of the array that hold values?
additionally if it does give me the entire array, what code would I use to return only the retreive the parts of the array that contain values such as ints? Moreover I am actually reading in using StringTokenizer and reading out data that can be broken up into two parts an opperator and an opperand. So, any helpful hints on using the StringTokenizer to read data from a JTextArea into an array and then output to get the opperator and opperand that would be most appriciated
thanks in advance,
Danny
+Pie Number of slices to send: Send
Arrays are incapable of knowing how many items they hold. When you do the for loop you wrote you will get everything out of the Array to include the null values. When you:

you create an Array that can hold up to 100 someType's, but at that point it contains 100 nulls.
You might consider using an ArrayList. This will give you a size() method that lets you know the number of elements.
An ArrayList can not store primitives so if you have primitives you would need to wrap them.
+Pie Number of slices to send: Send
If you want to retreive some part of array object, you can use System.arraycopy(...).
+Pie Number of slices to send: Send
I realize this is a theoretical question, but there are some problems with the code snippet. As in it won't compile or work as expected. 1. The int i in the for loop needs to be initialized. 2. someArray.length should be used rather than someArray[].length. 3. the loop test should be < rather than greater than since it's using "i++" and assuming i is initialized to 0.

You asked if it were possible to retrieve only the parts of the array that hold values and specifically mention ints. With an array of ints each int in the array is initialized to 0, they all hold values. With arrays of objects such as Integer initialization is to null and you could tell which parts of the array had real values.




give me the entire contents of an array or just the parts of the array that hold values?
additionally if it does give me the entire array, what code would I use to return only the retreive the parts of the array that contain values such as ints?
...



Here I've made the changes I suggested, perhaps this is what you meant.

[ March 10, 2005: Message edited by: Carol Enderlin ]
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 739 times.
Similar Threads
Array reflection in Java
Oh, God I NEED Help!!!!!!!!
to and fro out of a loop to a method in back?
Trials and tribulations, trys and catches
StringTokenizer Class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:10:15.