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