0

I need to create an array of integers who will include all numbers between 0 to 53, but some of this integers I want to exclude from the array. Is there any function to do it in Java?

I have an array with the exclusions. There is no logic behind this (In fact, there is a logic, but I don't know how to express this), thus I created an array to specify:

int[] exclusions = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};

I saw seen an example of something similar to what I want to do, but in C# (That question is about a random number, which is not what I want, but similar to).

BrunnoFdc
  • 113
  • 1
  • 4
  • I'm sorry for any grammar errors in the text. I'm newbie at English. – BrunnoFdc May 14 '19 at 00:09
  • `IntStream.range(0, 53).filter(i -> ...).toArray()` – shmosel May 14 '19 at 00:09
  • Your grammar seems fine. And what dictates what numbers will be removed? And the range is so small you could just create a list of numbers and remove from it as needed. Or maintain a set of excluded numbers and check it when necessary. – Carcigenicate May 14 '19 at 00:10
  • @Carcigenicate Thanks. There is no logic to dictate what numbers will be removed. I have an array with all exclusions I need (Forgot to include this in post, omg). But your ideia looks good, check the set of exclusions when necessary. If no solution is presented, I will use it. – BrunnoFdc May 14 '19 at 00:20
  • @Brunno shmosel's solution is fairly idiomatic. The filter function oukd define what's kept. I'd go with it. It would be a go way to learn streams too if you don't use them already. – Carcigenicate May 14 '19 at 00:22
  • you should come up with filtration logic which makes some sense to eliminate @Brunno – Ryuzaki L May 14 '19 at 00:28
  • @Carcigenicate Yess, I figured I could use stream, because I already knew the filter function, but the problem in this case was: "How to set the range?". Thanks shmosel. – BrunnoFdc May 14 '19 at 00:31

2 Answers2

1

You can create a set of numbers you want to exclude and filter the numbers produced by IntStream.range:

Set<Integer> exclusions = new HashSet<>();
// add numbers to set
...
int[] numbers = IntStream.range(0,53)
    .filter(n -> !exclusions.contains(n))
    .toArray();
Benjamin Urquhart
  • 1,626
  • 12
  • 18
1

You can do it several ways. First is using the concat feature of IntStream. The following produces a list of 0-5,10-15,and 20-25.

      int[] vals = IntStream.concat(
            IntStream.concat(IntStream.range(0, 6), IntStream.range(10, 16)),
            IntStream.range(20, 26)).toArray();
      System.out.println(Arrays.toString(vals));

You can also just include a list of those numbers to exclude. This example excludes those in the filter list.

      int[] vals2 = IntStream.range(0, 54).filter(
            a -> !List.of(10, 20, 30, 40).contains(a)).toArray();
      System.out.println(Arrays.toString(vals2));
WJS
  • 36,363
  • 4
  • 24
  • 39
  • 1
    Stream.of(Range A, Range B, Range C).flatMapToInt(x -> x) , whenever thinking about multiple streams , i feel its best to flatten – Yugansh May 14 '19 at 01:17
  • @Yugansh I would definitely go with your approach! – WJS May 14 '19 at 01:26