0

Can someone help me

I need help in Java, arrays, i need to extract all positive numbers form array to pArr and all negative numbers to new array called nArr and without 0

20th Dec 2016, 7:59 PM
Ljubisa Kuzmanovic
Ljubisa Kuzmanovic - avatar
2 Answers
+ 1
for (int I = 0; I < arr.length; i++){ if (i%2==0){ pArr [i] = arr [i]; }else { nArr [i] = arr [i]; } } not the best code but it works you will have a problem because this code will left put every secound index in the arrays but it depends on you how you won't to save the data, you could append the elements to avoid that or just divide I by 2 hope this helps ~Kamil
20th Dec 2016, 9:03 PM
Kamil
Kamil - avatar
+ 1
//assuming you already have an array 'nums' of type 'int' and it's already filled with numbers //as you don't know how much positive and negative numbers you have, you'll need an ArrayList type of pArr and nArr ArrayList<int> pArr = new ArrayList<int>; ArrayList<int> nArr = new ArrayList<int>; //next, loop through nums and check each number, adding it to a needed ArrayList for (int x: nums) { if (x > 0) { pArr.add(x); } else if (x < 0) { nArr.add(x); }
20th Dec 2016, 9:07 PM
Demeth
Demeth - avatar