• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

a simple array question...

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a String array like this:
String[] arrStr = {"123",null,null,"345",null,"fg",null}
if I turn it to like arrStr2 neither use loop to remove the null nor rebuild a arrstr array,how can I do :
arrStr2 = {"123","345","fg"}
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's probably a more efficient way, but you could do this:
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't think of a way off the top of my head to do it without a loop. Why don't you want to use a loop? Or did I just misunderstand the question?
 
zknyy wolf
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tahnk you.Lance Finney and Layne Lund
I do it as Lance do.but I want to find a simple way as use API like
Object[] removeObject(Object[],Object)
now I think it's impossible.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's quite some overkill, but not using (visible) loops can be done as follows:
String[] arr1= { ... };
List list= Arrays.asList(arr1);
List temp= Arrays.asList( new Object[] { null });
Object[] arr2= list.removeAll(temp).toArray();
The trouble is, you'll end up with an Object[], not a String[]. I think a simple (explicit) loop would be way more efficient.
kind regards
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create a String[] rather than Object[] by precreating the array with the correct size:

The final line of Lance Finney's solution works as well - I just hate creating that extra zero-length array when I don't have to.
Unfortunately Arrays.asList() creates a fixed-length list - which means that removeAll() throws an UnsupportedOperationException. . We can work around this by creating another List:

Of course, in the original question, "if I turn it to like arrStr2 neither use loop to remove the null nor rebuild a arrstr array" doesn't make much sense - there's no way to avoid "rebuilding" an array here, since the new array has a different size from the original. So I don't know if this answer is considered acceptable or not - but I'm not going to worry too much about it.
[ February 01, 2003: Message edited by: Jim Yingst ]
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic