TaeHyun. A TaeHyun. A - 2 months ago 5
Java Question

How to return specific values in an array? - Java

Simply, I have a set of numbers like this.

int targetNumber = 8;

int[] n = new int[4];
n[0] = 2;
n[1] = 4;
n[2] = 8;
n[3] = 16;


Now, I'm trying to return 3 numbers.

For example, since
n[2]
is equal to the
targetNumber
, I want to return
n[0]
,
n[1]
, and
n[3]
.

Any ways on how I can do that?

Answer

You can do it in classic fashion:

for (int i : n) {
    if (i != targetNumber)
        System.out.println(i);
}

output will be n[0], n[1] and n[3]

2
4
16
Comments