• Post Reply Bookmark Topic Watch Topic
  • New Topic

ARRAY QUESTION  RSS feed

 
DANIEL GRESHAM
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Surasak Leenapongpanit
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to retreive some part of array object, you can use System.arraycopy(...).
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!