简体   繁体   中英

How do I create a nested for loop using an array to pull specific numbers from a group of numbers in java?

So, I'm trying to create a nested for loop to pull all of the even numbers from a group of numbers but I want it to stop at 238 instead of going all the way through all of the numbers. Here is what I have gotten so far, but I feel like I'm doing something wrong. To be more specific, I need to be able to read all of the numbers listed below, and then pull all of the even numbers in the order that they are shown going no higher than 238.

public class test {
    public static void main(String[] args) {
        int[] numbers = {
            951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 
            615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 
            386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
            399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
            815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
            958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 
            743, 527};

        int[] arr = {};
        for (int i = 0; i < arr.length; i++) {
            int el = arr[i];
            System.out.println(el);
        }

        int[] input = {};
        for (int i = 0; i < 238; i++) {
            for (i = 0; i < 238; i++) {
                if ( i >= 237) {
                    break;
                }
            }
        }
    }
}

What about this one:

  • filter all values are less or equal than required ( max = 238 )
  • filter all even values

public static int[] findAllEvenNumbers(int[] arr, int max) {
    return IntStream.range(0, arr.length)
                    .filter(v -> v <= max)
                    .filter(v -> (v & 1) == 0).toArray();
}

So, I'm trying to create a nested for loop to pull all of the even numbers from a group of numbers but I want it to stop at 238 instead of going all the way through all of the numbers. Here is what I have gotten so far, but I feel like I'm doing something wrong. To be more specific, I need to be able to read all of the numbers listed below, and then pull all of the even numbers in the order that they are shown going no higher than 238.

public class test {
    public static void main(String[] args) {
        int[] numbers = {
            951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 
            615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 
            386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
            399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
            815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
            958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 
            743, 527};

        int[] arr = {};
        for (int i = 0; i < arr.length; i++) {
            int el = arr[i];
            System.out.println(el);
        }

        int[] input = {};
        for (int i = 0; i < 238; i++) {
            for (i = 0; i < 238; i++) {
                if ( i >= 237) {
                    break;
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM