• 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

How to find if elements of an array is null

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I am decalring following:

byte[] bb = new byte[512];

and want to know after some processing that if all elements of this bb byte array are null or not. Is there any way of finding the same except runnign a while loop?

regards
Arun
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No elements of this array will ever be null; only object references can be null, and a byte is not an object reference. If you want to find out if any elements are 0: yes, in general, you need to check with a loop.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, I think you would need to use a loop.... but here are some really bad alternatives, just for fun:


// Could probably be improved with some Pattern matching, and could
// definitely be improved with fewer (ideally 1) invocation of
// Arrays.toString().... but what fun would that be?
((Arrays.toString(bb).indexOf(" 0,") < 0) &&
(Arrays.toString(bb).indexOf("[0,") < 0) &&
(Arrays.toString(bb).indexOf(" 0]") < 0) &&
(!Arrays.toString(bb).equals("[0]"))


// Have to clone only if you want to maintain the original ordering
Arrays.sort(bb.clone()).binarySearch((byte)0)


// hrmmm.... I bet this is looping internally anyway...
Arrays.aslist(bb).contains((byte)0)


Alright, enough foolishness. Good luck,
-- Jon
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks fo ryour reply.

I also tried this way:

String str = new String(bb);

if(str.length()<1)
System.out.println("Array is empty");

Though seems a bit inefficient

//br Arun
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic