简体   繁体   中英

Java Array - sort numbers

I have an assignment to do where I need to take numbers from an array I was given, int[] array1 = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87}, and list them in the arrays: positive, negative and duplicate.

Here is the code I have so far, but I'm stuck with these duplicate numbers. I don't think I know how to get them from this array. I think I would need to check after I get the positive and negative arrays, but I'm not sure how. Any help would be appreciated. Thanks. {

int[] array1 = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87}, pos, neg, dup;
int positive = 0;
int negative = 0;
int duplicate = 0;


for (int i : array1)

{
    if (i >= 0)
    {
        positive++;
    } 
    else if (i < 0){

        negative++;
    }

    else 
    {
        duplicate++;
    }
}


pos = new int[positive];
neg = new int[negative];
dup = new int[duplicate];


positive = 0;
negative = 0;
duplicate = 0;


for (int i : array1)
{
    if (i >= 0)
    {
        pos[positive] = i;
        positive++;
    } 
    else if (i < 0)
    {
        neg[negative] = i;
        negative++;
    }
    else dup[duplicate] = i;
        negative++;
}

   System.out.print("\nPositive array: ");
for (int i: pos)
{
    System.out.print(" " + i);
}

System.out.print("\nNegative array: ");
for (int i: neg)
{
    System.out.print(" " + i);
}
System.out.print("\nDuplicate array: ");
for (int i: dup)
{
    System.out.print(" " + i);
}

} }

public void sortArrays(){

ArrayList<Integer> positiveList = new ArrayList<>();
ArrayList<Integer> negativeList = new ArrayList<>();

for(int i : array1){//getting all positive nums & negative nums
if(i >= 0)
positiveList.add(i);
else{
negativeList.add(i);
   }
}

int counter = 0;
ArrayList<Integer> duplicates = new ArrayList<>();//Will hold duplicates

for(int i : array1){//The actual check for duplicates
for(int k : array1){
if(i == k)
   counter++;
  }

if(counter > 1)
duplicates.add(i);
counter = 0;
  }
}

This type of logic in your code will never update the duplicate count because if i is not greater than or equal to 0 AND i is not strictly less than 0 , then what will i be?

//incorrect -> will never go into the last else
if (i >= 0)
{
    positive++;
} 
else if (i < 0){

    negative++;
}
else 
{
    duplicate++;
}

My suggestion is, initially, just put every number in a HashMap (convert the int to an Integer object first) where it is the key and the value should be its count. Then iterate the HashMap and find the positives, negatives and duplicates

You don't have to use for loop more than once. You may try with this code,

public static void main(String[] args) throws Exception {
    int[] array1 = { 12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87 };
    printSummary(array1);
}

public static void printSummary(int[] nums) {
    Set<Integer> foundNumbers = new HashSet<Integer>();
    List<Integer> positiveNums = new ArrayList<Integer>();
    List<Integer> negativeNums = new ArrayList<Integer>();
    List<Integer> duplicateNums = new ArrayList<Integer>();

    for (int i = 0; i < nums.length; i++) {
        if (nums[i] >= 0) {
            positiveNums.add(nums[i]);
        } else {
            negativeNums.add(nums[i]);
        }

        if (foundNumbers.contains(nums[i])) {
            duplicateNums.add(nums[i]);
        }
        foundNumbers.add(nums[i]);
    }
    System.out.println("Positive numbers: " + positiveNums + " (Total: " + positiveNums.size() + ")");
    System.out.println("Negative numbers: " + negativeNums + " (Total: " + negativeNums.size() + ")");
    System.out.println("Duplicate numbers: " + duplicateNums + " (Total: " + duplicateNums.size() + ")");
}

This gives following output,

Positive numbers: [12, 23, 0, 43, 545, 43, 12, 0] (Total: 8)
Negative numbers: [-22, -4, -55, -999, -87] (Total: 5)
Duplicate numbers: [43, 12, 0] (Total: 3)

Here is a solution using only basic arrays. I have introduced a result array where I keep track if the number in the original array is positive, negative or a duplicate.

int[] array1 = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
int[] result = new int[array1.length];

int value = 0;
for (int i = 0; i < array1.length; i++) {
    value = array1[i];
    if (value >= 0) {
        result[i] = 1;
    } else {
        result[i] = -1;
    }
}

for (int i = 0; i < array1.length; i++) {
    for (int j = i + 1; j < array1.length; j++) {
        if (array1[i] == array1[j]) {
            result[j] = 0;
        }
    }
}

System.out.print("\nPositive array: ");
for (int i = 0; i < result.length; i++) {
    if (result[i] == 1) {
        System.out.print(" " + array1[i]);
    }
}
System.out.print("\nNegative array: ");
for (int i = 0; i < result.length; i++) {
    if (result[i] == -1) {
        System.out.print(" " + array1[i]);
    }
}
System.out.print("\nDuplicate array: ");
for (int i = 0; i < result.length; i++) {
    if (result[i] == 0) {
        System.out.print(" " + array1[i]);
    }
}

Output is

Positive array: 12 23 0 43 545
Negative array: -22 -4 -55 -999 -87
Duplicate array: 43 12 0

you can use this

int[] pos=new int[array1.length];
int[] neg=new int[array1.length];
int[] zeros=new int[array1.length];
int[] l1=new int[array1.length];
int[] duplicate=new int[array1.length];

int number=0;
for(int i=0;i<=array1.length;i++){
    number=array1[i];
    if(l1.contains(number) && duplicate.contains(number))
        duplicate[duplicate.length+1]=number;
    else
        l1[l1.length+1]=number;
    if(number<0)
        neg[neg.length+1]=number;
    else if(number>0)
        pos[pos.length+1]=number;
    else
        zeros[zeros.length+1]=number;
}
int positive=pos.length();
int negative=neg.length();
int zero=zeros.length();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM