-1

I have a question and I will provide my code below. I need to target sequences to be as much as positive or negative members of the array. Can anyone tell me what I need to change in my code to do so?

import java.util.Scanner;

class DynamicArray {
    public static void main(String[] args) {    
        int i,a,b;
        System.out.println("Enter the limit of array :" );
        Scanner s = new Scanner(System.in);
        int limit = s.nextInt();
        int [] array1 = new int[limit];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87

        System.out.println("Enter the numbers");
        for(i=0; i<limit; i++)
        {
            array1[i] = s.nextInt();
        }
        int [] arrayPlus = new int[limit];
        int [] arrayMinus = new int[limit];

        a=b=0;

        for (i = 0; i < limit; i++) {
            if (array1[i] > 0 || array1[i] == 0) {
                arrayPlus[a] = array1[i];
                a++;
            } else {
                arrayMinus[b] = array1[i];
                b++;
            }
        }
        System.out.println("Positive array numbers");
        for (i = 0; i < a; i++) {
            System.out.println(arrayPlus[i]);}
        System.out.println("");

        System.out.println("Negative array numbers");
        for (i = 0; i < b; i++) {
            System.out.println(arrayMinus[i]);}
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
John Smith
  • 13
  • 1
  • 1
    I'm not sure what your question is. Please clarify. According to the title it could be a duplicate of this question: http://stackoverflow.com/questions/2426671/variable-length-dynamic-arrays-in-java – fabian Dec 18 '15 at 10:25
  • The problem is that Im not aloved to use ArrayList – John Smith Dec 18 '15 at 10:28

1 Answers1

-1

first of all you can simplify this:

if (array1[i] > 0 || array1[i] == 0) {

to

if (array1[i] >= 0) {

second one remove this variables. there are not necessary:

int i, a, b;

in each for loop define the type of i:

for (int i = 0;

next one you dont know the number of positive and negative numbers so I suggest you to use a List instead of arrays: change

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

to

  List<Integer> arrayPlus = new ArrayList<>();
  List<Integer> arrayMinus = new ArrayList<>();

add elements with:

arrayPlus.add(array1[i]);
arrayMinus.add(array1[i]);

and read elements with:

System.out.println(arrayPlus.get(i));
System.out.println(arrayMinus.get(i));

iterate over the lists with:

for (int i = 0; i < arrayPlus.size(); i++) {

OVERALL:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class DynamicArray {
  public static void main(String[] args) {

    System.out.println("Enter the limit of array :");
    Scanner s = new Scanner(System.in);
    int limit = s.nextInt();
    int[] array1 = new int[limit];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87

    System.out.println("Enter the numbers");
    for (int i = 0; i < limit; i++) {
      array1[i] = s.nextInt();
    }
    List<Integer> arrayPlus = new ArrayList<>();
    List<Integer> arrayMinus = new ArrayList<>();


    for (int i = 0; i < limit; i++) {
      if (array1[i] >= 0) {
        arrayPlus.add(array1[i]);
      } else {
        arrayMinus.add(array1[i]);
      }
    }
    System.out.println("Positive array numbers");
    for (int i = 0; i < arrayPlus.size(); i++) {
      System.out.println(arrayPlus.get(i));
    }
    System.out.println("");

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

OVERALL(without Lists)

import java.util.Scanner;

class DynamicArray {
  public static void main(String[] args) {
    int p=0,n=0;
    System.out.println("Enter the limit of array :");
    Scanner s = new Scanner(System.in);
    int limit = s.nextInt();
    int[] array1 = new int[limit];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87

    System.out.println("Enter the numbers");
    for (int i = 0; i < limit; i++) {
      int lInput = s.nextInt();
      if (lInput >= 0) {
        p++;
      } else {
        n++;
      }
      array1[i] = lInput; 
    }
    int[] arrayPlus = new int[p];
    int[] arrayMinus = new int[n];

    int indexPlus = 0;
    int indexMinus = 0;
    for (int i = 0; i < limit; i++) {
      if (array1[i] >= 0) {
        arrayPlus[indexPlus] = array1[i];
        indexPlus++;
      } else {
        arrayMinus[indexMinus] = array1[i];
        indexMinus++;
      }
    }
    System.out.println("Positive array numbers");
    for (int i = 0; i < arrayPlus.length; i++) {
      System.out.println(arrayPlus[i]);
    }
    System.out.println("");

    System.out.println("Negative array numbers");
    for (int i = 0; i < arrayMinus.length; i++) {
      System.out.println(arrayMinus[i]);
    }
  }
}
nano_nano
  • 12,351
  • 8
  • 55
  • 83