简体   繁体   中英

java arrays, executing positive and negative numbers

I need to execute positive and negative numbers from an array list. I also need to execute duplicates from array list. I will post my java code and hope someone can tell me why I can't run this code. Is there something missing in it? Thanks in advance.

 public static void main(String[] args) {

    int i, a, b;
    int[] array1 = new int[20];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87

    array1[0] = 12;
    array1[1] = 23;
    array1[2] = -22;
    array1[3] = 0;
    array1[4] = 43;
    array1[5] = 545;
    array1[6] = -4;
    array1[7] = -55;
    array1[8] = 43;
    array1[9] = 12;
    array1[10] = 0;
    array1[11] = -991;
    array1[12] = -87;

    int[] arrayPlus = new int[20];
    int[] arrayMinus = new int[20];


    a = b = 0;


    for (i = 0; i < 13; i++) {
        if (array1 > 0 || array1 == 0) {
            arrayPlus[a] = array1;
            a++;
        } else {
            arrayMinus = array1;
            b++;
        }

    }
    System.out.println("Positive array numbers");
    for (i = 0; i < a; i++) {
        System.out.println(arrayPlus);
    }


    System.out.println("");

    System.out.println("Negative array numbers");
    for (i = 0; i < b; i++) {
        System.out.println(arrayMinus);
    }

  }
}

You are comparing whole table array1 with integer. You can't do that. You should compare only one element of the array with 0. That mean you should use array1[i] instead.

Try to change this block:

  if (array1 > 0 || array1 == 0){
  arrayPlus[a] =array1;
  ...
 {arrayMinus =array1;
  ...
 }

for (i = 0; i < a; i++) {
System.out.println(arrayPlus);}

 System.out.println("Negative array numbers");
 for (i = 0; i < b; i++) {
 System.out.println(arrayMinus);}

 }

With this:

   if (array1[a] > 0 || array1[a] == 0){
   arrayPlus[a] =array1[a];
  ...
  {arrayMinus[a] =array1[a];

 ...
 for (i = 0; i < a; i++) {
System.out.println(arrayPlus[a]);}

  System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus[b]);}

}

And for more learn array go in link

Try to instead of code

for (i = 0; i < 13; i++) {
    if (array1 > 0 || array1 == 0) {
        arrayPlus[a] = array1;
        a++;
    } else {
        arrayMinus = array1;
        b++;
    }

}

Use following code:

for (i = 0; i < 13; i++) {
    if (array1[i] > 0 || array1[i] == 0) {
        arrayPlus[a] = array1[i];
        a++;
    } else {
        arrayMinus[b] = array1[i];
        b++;
    }
}

You should work with elements of arrays (array1[i], arrayMinus[b]) not with whole arrays (array1, arrayMinus). Some problem with code:

for (i = 0; i < a; i++) {
    System.out.println(arrayPlus); // use arrayPlus[i]
}


System.out.println("");

System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
    System.out.println(arrayMinus); // use arrayMinus[i]
}

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