Java数组-排序数字

时间:2018-10-19 19:19:09

标签: java arrays

我有一个分配要做的工作,需要从给出的数组中获取数字,int [] array1 = {12,23,-22,0,43,545,-4,-55,43,12 ,0,-999,-87},并在数组中列出它们:正数,负数和重复项。

这是我到目前为止的代码,但是我被这些重复的数字所困扰。我认为我不知道如何从此阵列中获取它们。我想在得到正负数组后需要检查,但我不确定如何。任何帮助,将不胜感激。谢谢。 {

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);
}

}  }

5 个答案:

答案 0 :(得分:0)

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;
  }
}

答案 1 :(得分:0)

您的代码中的这种逻辑永远不会更新重复计数,因为如果 i不大于或等于 0并且 i不小于0 ,那我会是什么?

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

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

我的建议是,最初,只需将每个数字放在HashMap中(首先将int转换为Integer对象),该数字就是键,值应该是其计数。然后迭代HashMap并找到肯定,否定和重复项

答案 2 :(得分:0)

您不必多次使用for循环。您可以尝试使用此代码,

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() + ")");
}

这将提供以下输出,

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)

答案 3 :(得分:0)

这里是仅使用基本数组的解决方案。我介绍了一个结果数组,该数组可以跟踪原始数组中的数字是正数,负数还是重复数。

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]);
    }
}

输出为

  

正数组:12 23 0 43 545
  负数组:-22 -4 -55 -999 -87
  重复数组:43 12 0

答案 4 :(得分:-1)

您可以使用此

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();