通过两个整数数组排序正负整数

时间:2015-05-16 10:30:24

标签: java arrays sorting

我们提供了以下字符串:

int [] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87}.

必须有两行,一行用于存储,另一行用于字符串数组的负元素,并提取必要的逻辑,执行相应的元素并将它们放在适当的线程中。

换句话说,在给定的行数组中,将获得所有正元素并将其放置在单独的行中。此外,要获取的所有元素和要放在单独行中的否定。

您还需要确定字符串数组中的重复数量。

当然,编写的必要功能适用于任意数量的整数。但我的老师说我的代码没有目标行,他给了我一个例子:目标字符串是另外两行,你将放置隔离的正负值。例如,如果您有一个字符串:

int arr = {1,2,3,4, -1, -2, -3, -4}

然后目标范围将是:

int pos = {1,2,3,4}
int hc = {-1, -2, -3, -4}

当然,这应该使它可编程而不是硬编码,如本例所示。现在我该如何改进我的代码?

public static void main(String[] args) {

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

    System.out.println("Array 1 :");

    Arrays.sort(array1);

    for (int positive: array1) {

        if (positive >= -1)
            System.out.println("Positive numbers :" + positive+ "\t");
        }
        System.out.println();
        System.out.println("Array 2 :");

        for (int negative: array1) {
            if (negative >= -1) {

            }else{ System.out.println("Negative numbers :" +negative);
            }

        } 
        System.out.println();

        for (int i = 0; i < array1.length -1; i++) { 
             if (array1[i + 1 ] == array1[i]) {
                 System.out.println("Duplicate element found :" + array1[i]);
                 i = i + 1;

             }          

        }

    }           
}

3 个答案:

答案 0 :(得分:1)

如果我已正确理解你的问题,你想制作包含正数和负数的新数组,

使用你的变量名称我会选择这样的解决方案:

public static void main(String[] args) {

    // Declare variables
    int[] array1 = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87}, pos, hc;
    int positive = 0, negative = 0;

    // Check how many positive and/or negative numbers
    for (int i : array1)
    {
        if (i >= 0)
        {
            positive++;
        } else 
        {
            negative++;
        }
    }

    // Make exact size arrays
    pos = new int[positive];
    hc = new int[negative];

    // Reset variables for new purpose
    positive = 0;
    negative = 0;

    //Put numbers in correct array
    for (int i : array1)
    {
        if (i >= 0)
        {
            pos[positive] = i;
            positive++;
        } else
        {
            hc[negative] = i;
            negative++;
        }
    }

    // Display arrays
    System.out.print("Starter array: ");
    for (int i: array1)
    {
        System.out.print(" " + i);
    }

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

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

}

输出:

Starter array:12 23 -22 0 43 545 -4 -55 43 12 0 -999 -87

正数组:12 23 0 43 545 43 12 0

负数组:-22 -4 -55 -999 -87

答案 1 :(得分:0)

尝试

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

 System.out.println("Array 1 :");

 Arrays.sort(array1);
 ArrayList<Integer> pos = new ArrayList<Integer>();
 ArrayList<Integer> neg = new ArrayList<Integer>();
 for (int num: array1){
  if (num>= 0)
       pos.add(num);
   else
       neg.add(num);
}
 System.out.println();

 if(pos.size()>0)
 {
 int[] positive = new int[pos.size()];
 positive = pos.toArray(positive);
 pos=null;
 for (int num: positive)
      System.out.println("Positive numbers :" + num+ "\t");
 }

 if(neg.size()>0)
 {
 int[] negative = new int[neg.size()];   
 negative = pos.toArray(negative);
 neg=null;
 for (int num: negative)
      System.out.println("Negative numbers :" + num+ "\t");
 }

for (int i = 0; i < array1.length -1; i++) { 
    if (array1[i + 1 ] == array1[i]) {
        System.out.println("Duplicate element found :" + array1[i]);
        i = i + 1;

        }          

    }

答案 2 :(得分:0)

   sepratePositiveNegative(int[] arr) {
        int size  = arr.length;
        int k =0;
        for(int i =0;i <size; i++){
            int temp = 0;
            if(arr[i]<0) {
                temp = arr[i];
                arr[i] = arr[k];
                arr[k] = temp;
                k++;
                System.out.println(arr[i]);
            }
        }