Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Sparse Array Detection

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Supposed I'm to process an array of class A.
And I want to process it differently if its sparse
or not sparse. ("Sparse" meaning some items are null)
Is there something in the reflection API or normal java.lang
that will tell me whether an array is sparse or not ?
P/S I'd rather not have to write my own routine to iterate
through the whole array.
Thanks
Pho
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi-
You can use this, it is a little messy but it will work.
import java.util.*;
....
Object[] thisArray = new Object[1000];
...
//populate the array with the items
List tmpList = Arrays.asList(thisArray);
int numItems = tmpList.size();
/*this gives you your number of items in the array. You can then go ahead and compare it to the thisArray.length number to dicide which path you want to take.*/
It would probably be easier if you want to do any amount of operating on arrays to use the java.util.ArrayList class instead.
Kyle
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kyle,
I tried your suggestions but it didn't work.
My code:

Any other suggestions?
Pho
[This message has been edited by Pho Tek (edited August 04, 2001).]
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since there doesn't seem to be any real way of do this you need to start cheating.
The first cheat that I thought of was to save the number of elements in the array in position [ 0 ] of the same array. You would do that as you are loading the array with data.
(Does that make sense?)
 
kyle amburn
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pho-
Sorry about that. I forgot that the list would take the nulls and place them as objects. You can use the method from Christoper or you can try the following. I ran this and got the correct values (i.e. i item, 3 spaces).

Kyle
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vector.remove() in a loop ? This is very inefficient - think how many passes through a 1000 element Vector would be needed where just the last 100 are null. The question was could it be done avoiding writing your own routine, so perhaps the answer is simply 'no', and counting the number of null elements might be better in a for loop (you could even 'break' as soon as you found one).
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey people,
thanks for all your suggestions.
I actually followed the path suggested by Colin and wrote
my own isSparse method by iterating thru the array. It was
then that I was shocked to find that the array that I was
operating on is an array value, an Object instead of Object[]. So I had to use java.lang.reflect.Array to process it. Messy, but I'll live with it for the moment.
Pho
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic