473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

push all the non zero elements

Can any one tell me how to push all the non zero elements to one end of an array in java without creating a new array.
Mar 8 '12 #1
2 1666
r035198x
13,262 8TB
If you really don't want to create a new array then use bubble sort.

Better remove that restriction and use an OO approach like

Expand|Select|Wrap|Line Numbers
  1. static class NullComparator<T extends Number> implements Comparator<T> {
  2.     @Override
  3.     public int compare(T o1, T o2) {
  4.         if (o1 == null || o1.intValue() == 0) {
  5.         return -1;
  6.         }
  7.  
  8.         return 1;
  9.     }
  10.     }
  11.     public static void main(String[] args) {
  12.     Long[] arrayToSort = new Long[] { null, 42l, null, 0l, 24l, 0l, 42l };
  13.     Arrays.sort(arrayToSort, new NullComparator());
  14.     System.out.println(Arrays.toString(arrayToSort));
  15.     }
  16.  
Mar 8 '12 #2
i got it.. thank you for your reply..
Mar 8 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Christopher Benson-Manica | last post by:
When are named elements written with script accessible to script? <html><head><script type="text/javascript"> function ready() { alert( document.getElementsByName("div").length ); }...
3
by: Christopher Benson-Manica | last post by:
I appreciate all the responses to my earlier post about accessing named elements. However, I'm still wondering about my actual problem, which is that I need to initialize some arrays of named...
4
by: ioneabu | last post by:
The below function attempts to remove the contents of one form and replace with the contents of another. Is this not possible? Why not? It will make a big difference in the final solution to a...
9
by: John T. | last post by:
If I have: struct test{ unsigned short test1; unsigned short test2; unsigned short test3; unsigned char test4; unsigned char test5; }; int main(){
10
by: Sek | last post by:
I have a property that returns a ArrayList object. On failure condition, is it right to return null or return an ArrayList object with zero elements?
3
by: Frances | last post by:
how to do you add elements to an array? am looking at section dealing with arrays in JS Bible, can't find how you add to an array.. in the last few years I've been doing much more Java than JS, am...
11
by: David | last post by:
Hi Everyone, I'm trying to grab all of the elements of a DL, specifically the <a href>'s grouping them by the DD's. I suppose if I can just get them into groups I can get the href's later. The...
4
by: deLenn | last post by:
Hi, Does scipy have an equivalent to Matlab's 'find' function, to list the indices of all nonzero elements in a sparse matrix? Cheers.
19
by: nileshsimaria | last post by:
Hi, I have seen some code were we have array with zero elements (mostly within structure) like, typedef struct foo { int data }foo;
0
by: George3 | last post by:
Hello everyone, Sometimes, we allocate array of zero elements. I am wondering for what regualr purpose will we do that? I have tested that in Visual Studio 2008, it runs ok. Code:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.