• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with int[ ]

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestions how I can create an int array without initially defining it's size? The problem is that I don't know the size of the int array until after I've looped through a collection, but I need to store the ints (x) while looping through the collection in this int array.

If I initialize it with a bogus size, the int array has 0 values which causing another issue. Therefore I need to put only valid values in this array, no zeros unless it's a valid zero.

Here's some sample code if I'm too vague.



Thanks in advance for everyone's help!
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I often make an ArrayList. If I need an actual array for something, I can always get one with toArray()...

What are we doing geeking out on a Saturday night? (I'm escaping from my two boys who have two friends over and debating whether to give them all Benadryl).

Mark
 
Shannon Sims
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Well unfortunately, I cannot use an ArrayList. Got any other ideas.

Yes, unfortunately I'm geeking out on Saturday only because I've put off working on this project way too long. So I am buckling down and working no matter what day it is.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't define a variable-size array, java arrays are fixed length. However, using your recordsFound array as temporary storage (initialised to the full length of your collection), you can fill it from position 0 up and keep track of how many array elements are valid. Then all you have to do is copy the valid bit over into a new array.


int[] recordsFound = new int[ classSize ];
int lastValidIndex = -1;
int y = 0;
for( int x = 0; x < classSize; x++ )
{
String[] record = read( x );
if( record[0].startsWith( criteria[0] )
&& record[1].startsWith( criteria[2] ) )
{
recordsFound[ ++lastValidIndex ] = x;
}
}

int[] finalResult;
if (lastvalidIndex > -1) {
finalResult = new int[lastValidIndex + 1];
System.arraycopy(recordsFound, 0, finalResult, 0, finalResult.length);
}
else
{
finalResult = new int[0];
}



(edit) I had to remove the [code../code] block because it ate my boldface highlights.

(edit #2) The forum engine ate my explanation, AND there was a bug in my code snippet. Note that the if (lastValidIndex > -1) condition isn't even necessary (code creates a 0-length array and copies 0 elements into the new array if it's not there), but I wrote it out explicitly for clarity or in case you have to throw an exception or something like that.

Hope this helps
[ July 24, 2005: Message edited by: Barend Garvelink ]
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Well unfortunately, I cannot use an ArrayList. Got any other ideas.



Of course, now we're all curious as to why you can't use an ArrayList... Don't tell us it's a homework assignment! Could you use a Vector? If not, you've gone into a realm where this "Semi-Penitent C++ Convert Who Occasionally Thinks He Could Use Pointer" doesn't have an Elegant Solution:



Mark
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a nit: I'm occasionally told things like "If it was a snake, it woulda bit you!", so I probably missed it, but I don't see where you increment y in your original code...

Mark
 
Barend Garvelink
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Wuest:


Mark



Mark,


Two things:
1) this code will throw an ArrayIndexOutOfBounds exception courtesy of the less-than-or-equal operator you used in the middle section of the for-loop.
2) for every element in the loop, this code will do the "array index within bounds" checking. If you use System.arraycopy(Object,int,Object,int,int) instead, the JVM will perform bounds-checking only once.
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barend Garvelink:


Mark<hr></blockquote>

Mark,


Two things:
1) this code will throw an ArrayIndexOutOfBounds exception courtesy of the less-than-or-equal operator you used in the middle section of the for-loop.
2) for every element in the loop, this code will do the "array index within bounds" checking. If you use System.arraycopy(Object,int,Object,int,int) instead, the JVM will perform bounds-checking only once.[/QB]



1) Yup - that's what I get fer writing from the seat of my pants
2) Yup - ditto, I knew that was in there, but couldn't remember where or what it was called! I think we did their homework assignment for them.

Mark
 
Barend Garvelink
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Wuest:
I think we did their homework assignment for them.



Uh-huh .



No arraylists could be J2ME, of course .
[ July 24, 2005: Message edited by: Barend Garvelink ]
 
Shannon Sims
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Sorry for the lack of response, however I need a break. When you code M-F, the last thing you want to do is spend the entire weekend coding again.

Actually, I'm working on my SCJD certification. One of the requirements is to implemented a method that returns an int array. Basically, I wanted to ensure that the only options I had were to initialize the array or new the int array with a predefined size. So, I guess with all the feedback I got, I will have to store the int values in some kind of collection and then dump them into an int array. Oh well...

Thank you all for your help!
 
Shannon Sims
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
Ah, it looks like I've forgotten to increment my "y". Thanks for bringing that to my attention. It's been hard working on my project using TextPad.

Thanks!
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic