Kristjan Tampere
Kristjan Tampere

Reputation: 45

Return all negative elements of a array

Okay, so i need to find all the negative numbers of array and return them.I found the negative number, but how do i return them all? P.S yes i am a beginner.

public static void main(String[] args) {
        int [] array = {5,-1,6,3,-20,10,20,-5,2};
        System.out.println(findNumber(array));
    }




public static int findNumber(int[] sum) {
    int num = 0;
    for (int i = 0; i < sum.length ; i++) {
        if(sum[i] < num) {
            num = sum[i];
        }
    }
    return num;
}

Upvotes: 0

Views: 7909

Answers (6)

user16123931
user16123931

Reputation:

public static int[] findNum(int[] array)
{
    int negativeIntCount = 0;
    int[] negativeNumbers = new int[array.length];
    for(int i = 0; i < array.length; i++)
    {
        if(array[i] < 0)
        {
            negativeIntCount++;
            negativeNumbers[i] = array[i];
        }
    }

    System.out.println("Total negative numbers in given arrays is " + negativeIntCount);

    return negativeNumbers;
}

To display as an array in output :

System.out.println(Arrays.toString(findNum(array)));

To display output as space gaped integers :

for(int x : findNum(array))
{
    System.out.print(" " + x)
}

Upvotes: 0

MiguelKVidal
MiguelKVidal

Reputation: 1538

You code is returning the sum of elements, but I understood that you wanted every negative number. So, I assumed you want something like this:

public static void main(String[] args) {
  int [] array = {5,-1,6,3,-20,10,20,-5,2};
  Integer [] result = findNumbers( array );
  for( int i : result )
  {
    System.out.println( i );
  }
}
public static Integer[] findNumbers(int[] v) {
  List<Integer> list = new ArrayList<>();
  for (int i = 0; i < v.length ; i++) {
      if(v[i] < 0) {
          list.add(v[i]);
      }
  }
  return list.toArray( new Integer[0] );
}

Is it?

Best regards.

Upvotes: 0

SkrewEverything
SkrewEverything

Reputation: 2523

As you told you are beginner, i'm giving code in using arrays only.

Whenever you come across a negative number, just add it to the array and increment it's index number and after checking all the numbers, return the array and print it.

public static void main(String[] args)
{
    int [] array = {5,-1,6,3,-20,10,20,-5,2};
    int[] neg = findNumber(array);
    for(int i = 0 ; i<neg.length; i++)
    {
        System.out.println(neg[i]);
    }
}

public static int[] findNumber(int[] a)
{
    int j=0;
    int[] n = new int[a.length];
    for(int i = 0; i<a.length ; i++)
    {
        if(a[i] <0)
        {
            n[j] = a[i];
            j++;
        }
    }

    int[] neg = new int[j];
    for( int k = 0 ; k < j ; k++)
    {
        neg[k] = n[k];
    }

    return neg;
}

I hope it helps.

Upvotes: 1

Dust_In_The_Wind
Dust_In_The_Wind

Reputation: 3692

You can modify your method to iterate through the array of numbers, and add every negative number you encounter, to a List.

public static List<Integers> findNegativeNumbers(int[] num) {
    List<Integers> negativeNumbers = new ArrayList<>();
    for (int i = 0; i < num.length; i++) {
        if(num[i] < 0) {
            negativeNumbers.add(num[i]);
        }
    }
    return negativeNumbers;
}

You could then print out the list of negative numbers from this method itself, or return the list with return to be printed in main.

Upvotes: 0

Matthew Brzezinski
Matthew Brzezinski

Reputation: 1794

There are multiple ways of doing this, if you just want to output all of the negative numbers easily you could do this:

public static void main(String[] args) {
    int [] array = {5,-1,6,3,-20,10,20,-5,2};
    ArrayList<Integer> negativeNumbers = findNumber(sum);
    for(Integer negNum : negativeNumbers) {
        System.out.println(negNum);
    }
}

public static ArrayList<Integer> findNumber(int[] sum) {
    ArrayList<Integer> negativeNumbers = new ArrayList<>();
    for (int i = 0; i < sum.length ; i++) {
        if(sum[i] < 0) {
          negativeNumber.add(sum[i]);
        }
    }
    return negativeNumbers;
}

Upvotes: 2

Raghav
Raghav

Reputation: 4638

Java 8 based solution. You can use stream to filter out numbers greater than or equal to zero

    public static int[] findNumber(int[] sum)
    {
        return Arrays.stream(sum).filter(i -> i < 0).toArray();
    }

Upvotes: 6

Related Questions