Reputation: 1
public class OddsAndEvens {
// counts all the odd numbers in the array
private static int countOdds(int[] array) {
int count = 0;
for(int i = 0; i < array.length; i++) {
if(array[i] % 2 == 1 && array[i] % 2 != 0) {
count++;
}
}
return count;
}
// returns an array with all the odd numbers
public static int[] getAllOdds(int[] array) {
int[] yaArray = new int[countOdds(array)];
int j = 0;
for(int i = 0; i < array.length; i++) {
if(array[i] % 2 == 1 && array[i] % 2 != 0) {
yaArray[j] = array[i];
}
j++;
}
return yaArray;
}
}
///////////////////////////////////////////////////////////
runner code
public class OddsAndEvensRunner {
public static void main(String args[]) {
System.out.println("Odds - " + Arrays.toString(OddsAndEvens.getAllOdds(new int[]{2,4,6,8,10,12,14})));
System.out.println("Evens - " + Arrays.toString(OddsAndEvens.getAllEvens(new int[]{2,4,6,8,10,12,14})));
System.out.println("\nOdds - " + Arrays.toString(OddsAndEvens.getAllOdds(new int[]{1,2,3,4,5,6,7,8,9})));
System.out.println("Evens - " + Arrays.toString(OddsAndEvens.getAllEvens(new int[]{1,2,3,4,5,6,7,8,9})));
System.out.println("\nOdds - " + Arrays.toString(OddsAndEvens.getAllOdds(new int[]{2,10,20,21,23,24,40,55,60,61})));
System.out.println("Evens - " + Arrays.toString(OddsAndEvens.getAllEvens(new int[]{2,10,20,21,23,24,40,55,60,61})));
}
}
Ignore code relating to evens. When run the odd array only lists out one odd number instead of the others inside of the first array along with a couple of zeros. I tried a lot of things but simply won't count all of the odd numbers.
Upvotes: 0
Views: 1275
Reputation: 11
Use List instead of Arrays. List is better to use.
Try like this,
public static void main(String[] args) {
System.out.println("Odds - " + Arrays.toString(getAllEvens(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
System.out.println("Evens - " + Arrays.toString(getAllOdds(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })));
}
public static Object[] getAllOdds(int[] array) {
List<Integer> list = new ArrayList<>();
for (int no : array) {
if (no % 2 != 0)
list.add(no);
}
return list.toArray();
}
public static Object[] getAllEvens(int[] array) {
List<Integer> list = new ArrayList<>();
for (int no : array) {
if (no % 2 == 0)
list.add(no);
}
return list.toArray();
}
You can also return list
instead of list.toArray()
for better list.
Upvotes: 0
Reputation: 7197
Take a look at this part:
public static int[] getAllOdds(int[] array) // when
{
int[] yaArray = new int[countOdds(array)];
int j = 0;
for(int i = 0; i<array.length;i++)
if(array[i]%2==1 && array[i]%2!=0)
yaArray[j]=array[i];
j++;
return yaArray;
}
The variable j
is incremented only once, which is after the loop ends.
If you have multiple statements to be executed in looping or conditional branch, use brackets.
Also array[i]%2==1
means the same thing as array[i]%2!=0
, so can eliminate one of them.
public static int[] getAllOdds(int[] array) // when
{
int[] yaArray = new int[countOdds(array)];
int j = 0;
for(int i = 0; i<array.length;i++)
{
if(array[i]%2==1)
{
yaArray[j]=array[i];
j++;
}
}
return yaArray;
}
Upvotes: 3