I have 3 arrays, oddList, negativeList and evenList that are taking numbers from the masterArray and sorting them accordingly. When I go to print the 3 arrays if some of the indexes in the array are not filled it is automatically filled with a 0.0 I need to figure out how to get rid of those zeros and only use the logical size of the array.
public static void negoddeven(double masterArray[]){
int z;
int size = 0;
int sum = 0;
double[] negativeList = new double[10], oddList = new double[10], evenList = new double[10];
for(z = 0; z < masterArray.length; z ){
for(int i = 0; i < size; i ){
if(masterArray[z]%2 != 0){
sum = oddList[z];
if(size < oddList.length){
oddList[z] = masterArray[z];
size ;
}}
else if(masterArray[z] < 0){
if(size < negativeList.length){
negativeList[size] = masterArray[z];
size ;
}}
else if(masterArray[z]%2 == 0){
if(size < negativeList.length){
evenList[size] = masterArray[z];
size ;
}}
}
}
System.out.print("\nThe numbers in the odd list are: " Arrays.toString(oddList));
System.out.print("\nThe numbers in the negative list are: " Arrays.toString(negativeList));
System.out.print("\nThe numbers in the even list are: " Arrays.toString(evenList) "\n");
}
CodePudding user response:
It would be more convenient to use dynamic List
instead of fixed-size arrays to collect all the data in masterArray
.
Also, negative values should be counted separately from the parity check.
public static void negOddEven(double ... masterArray) {
int sum = 0;
List<Double>
negativeList = new ArrayList<>(),
oddList = new ArrayList<>(),
evenList = new ArrayList<>();
for (int i = 0; i < masterArray.length; i ) {
double z = masterArray[i];
if (z < 0) {
negativeList.add(z);
}
if (z % 2 == 0) {
evenList.add(z);
} else {
oddList.add(z);
sum = z;
}
}
System.out.println("The numbers in the odd list are: " oddList "; sum = " sum);
System.out.println("The numbers in the negative list are: " negativeList);
System.out.println("The numbers in the even list are: " evenList "\n");
}
Test:
negOddEven(1, 2, 3, 0, 4.5, -2, -0.0, -12.2);
Output:
The numbers in the odd list are: [1.0, 3.0, 4.5, -12.2]; sum = -4
The numbers in the negative list are: [-2.0, -12.2]
The numbers in the even list are: [2.0, 0.0, -2.0, -0.0]
If lists are not allowed due to some limitation and only arrays should be used, separate counters for the actual lengths of all arrays need to be introduced. Also it could make sense to cut off unused cells in the arrays using Arrays.copyOf
:
public static void negOddEven(double ... masterArray) {
int sum = 0;
double[]
negList = new double[10],
oddList = new double[10],
evenList = new double[10];
int negSz = 0, oddSz = 0, evenSz = 0;
for(int i = 0; i < masterArray.length; i ) {
double z = masterArray[i];
if (z < 0) {
if (negSz < negList.length) {
negList[negSz ] = z;
}
}
if (z % 2 == 0) {
if (evenSz < evenList.length) {
evenList[evenSz ] = z;
}
} else if (oddSz < oddList.length) {
oddList[oddSz ] = z;
sum = z;
}
}
if (negSz < 10) negList = Arrays.copyOf(negList, negSz);
if (oddSz < 10) oddList = Arrays.copyOf(oddList, oddSz);
if (evenSz < 10) evenList = Arrays.copyOf(evenList, evenSz);
System.out.println("The numbers in the odd list are: " Arrays.toString(oddList) "; sum = " sum);
System.out.println("The numbers in the negative list are: " Arrays.toString(negList));
System.out.println("The numbers in the even list are: " Arrays.toString(evenList) "\n");
}