简体   繁体   中英

How to convert some elements that satisfy a particular condition in a 2d array into a list in java?

import java.util.Iterator;

import java.util.List;

import java.util.ArrayList;

class onomah {

    public static int[][] msp = new int[21][10];

        public static void main(String[] args) {


    msp[0][0] = 5;
    msp[0][1] = 10;
    msp[0][2] = 6;
    msp[0][3] = 7;
    msp[0][4] = 74;
    msp[0][5] = 85;
    msp[0][6] = 46;
    msp[0][7] = 38;
    msp[0][8] = 13;
    msp[0][9] = 47;
    msp[1][0] = 69;
    msp[1][1] = 20;
    msp[1][2] = 27;
    msp[1][3] = 67;
    msp[1][4] = 12;
    msp[1][5] = 1;
    msp[1][6] = 15;
    msp[1][7] = 25;
    msp[1][8] = 39;
    msp[1][9] = 65;
    msp[2][0] = 11;
    msp[2][1] = 1;
    msp[2][2] = 6;
    msp[2][3] = 30;
    msp[2][4] = 24;
    msp[2][5] = 68;
    msp[2][6] = 9;
    msp[2][7] = 52;
    msp[2][8] = 21;
    msp[2][9] = 83;
    msp[3][0] = 90;
    msp[3][1] = 21;
    msp[3][2] = 37;
    msp[3][3] = 9;
    msp[3][4] = 51;
    msp[3][5] = 36;
    msp[3][6] = 86;
    msp[3][7] = 23;
    msp[3][8] = 27;
    msp[3][9] = 64;
    msp[4][0] = 32;
    msp[4][1] = 5;
    msp[4][2] = 47;
    msp[4][3] = 68;
    msp[4][4] = 14;
    msp[4][5] = 55;
    msp[4][6] = 30;
    msp[4][7] = 26;
    msp[4][8] = 33;
    msp[4][9] = 67;
    msp[5][0] = 17;
    msp[5][1] = 54;
    msp[5][2] = 85;
    msp[5][3] = 86;
    msp[5][4] = 22;
    msp[5][5] = 72;
    msp[5][6] = 68;
    msp[5][7] = 80;
    msp[5][8] = 12;
    msp[5][9] = 27;
    msp[6][0] = 85;
    msp[6][1] = 21;
    msp[6][2] = 12;
    msp[6][3] = 37;
    msp[6][4] = 66;
    msp[6][5] = 73;
    msp[6][6] = 45;
    msp[6][7] = 44;
    msp[6][8] = 6;
    msp[6][9] = 81;
    msp[7][0] = 46;
    msp[7][1] = 64;
    msp[7][2] = 47;
    msp[7][3] = 63;
    msp[7][4] = 39;
    msp[7][5] = 3;
    msp[7][6] = 2;
    msp[7][7] = 18;
    msp[7][8] = 21;
    msp[7][9] = 36;
    msp[8][0] = 5;
    msp[8][1] = 87;
    msp[8][2] = 70;
    msp[8][3] = 17;
    msp[8][4] = 51;
    msp[8][5] = 88;
    msp[8][6] = 44;
    msp[8][7] = 15;
    msp[8][8] = 50;
    msp[8][9] = 25;
    msp[9][0] = 1;
    msp[9][1] = 47;
    msp[9][2] = 35;
    msp[9][3] = 54;
    msp[9][4] = 81;
    msp[9][5] = 82;
    msp[9][6] = 24;
    msp[9][7] = 79;
    msp[9][8] = 5;
    msp[9][9] = 71;
    msp[10][0] = 73;
    msp[10][1] = 78;
    msp[10][2] = 33;
    msp[10][3] = 83;
    msp[10][4] = 15;
    msp[10][5] = 17;
    msp[10][6] = 61;
    msp[10][7] = 45;
    msp[10][8] = 74;
    msp[10][9] = 1;
    msp[11][0] = 16;
    msp[11][1] = 53;
    msp[11][2] = 68;
    msp[11][3] = 90;
    msp[11][4] = 10;
    msp[11][5] = 23;
    msp[11][6] = 78;
    msp[11][7] = 40;
    msp[11][8] = 76;
    msp[11][9] = 37;
    msp[12][0] = 5;
    msp[12][1] = 87;
    msp[12][2] = 65;
    msp[12][3] = 12;
    msp[12][4] = 80;
    msp[12][5] = 88;
    msp[12][6] = 83;
    msp[12][7] = 64;
    msp[12][8] = 76;
    msp[12][9] = 36;
    msp[13][0] = 1;
    msp[13][1] = 40;
    msp[13][2] = 33;
    msp[13][3] = 35;
    msp[13][4] = 75;
    msp[13][5] = 20;
    msp[13][6] = 48;
    msp[13][7] = 5;
    msp[13][8] = 18;
    msp[13][9] = 53;
    msp[14][0] = 82;
    msp[14][1] = 87;
    msp[14][2] = 3;
    msp[14][3] = 32;
    msp[14][4] = 9;
    msp[14][5] = 64;
    msp[14][6] = 75;
    msp[14][7] = 18;
    msp[14][8] = 40;
    msp[14][9] = 27;
    msp[15][0] = 29;
    msp[15][1] = 8;
    msp[15][2] = 42;
    msp[15][3] = 9;
    msp[15][4] = 84;
    msp[15][5] = 71;
    msp[15][6] = 54;
    msp[15][7] = 38;
    msp[15][8] = 7;
    msp[15][9] = 2;
    msp[16][0] = 79;
    msp[16][1] = 65;
    msp[16][2] = 38;
    msp[16][3] = 55;
    msp[16][4] = 54;
    msp[16][5] = 64;
    msp[16][6] = 8;
    msp[16][7] = 53;
    msp[16][8] = 48;
    msp[16][9] = 43;
    msp[17][0] = 74;
    msp[17][1] = 20;
    msp[17][2] = 88;
    msp[17][3] = 47;
    msp[17][4] = 66;
    msp[17][5] = 23;
    msp[17][6] = 40;
    msp[17][7] = 58;
    msp[17][8] = 34;
    msp[17][9] = 90;
    msp[18][0] = 36;
    msp[18][1] = 1;
    msp[18][2] = 9;
    msp[18][3] = 33;
    msp[18][4] = 24;
    msp[18][5] = 90;
    msp[18][6] = 72;
    msp[18][7] = 58;
    msp[18][8] = 60;
    msp[18][9] = 21;
    msp[19][0] = 11;
    msp[19][1] = 19;
    msp[19][2] = 54;
    msp[19][3] = 76;
    msp[19][4] = 34;
    msp[19][5] = 74;
    msp[19][6] = 27;
    msp[19][7] = 31;
    msp[19][8] = 20;
    msp[19][9] = 17;
    msp[20][0] = 52;
    msp[20][1] = 17;
    msp[20][2] = 44;
    msp[20][3] = 74;
    msp[20][4] = 51;
    msp[20][5] = 76;
    msp[20][6] = 79;
    msp[20][7] = 35;
    msp[20][8] = 18;
    msp[20][9] = 54;

    roota0();

 }

 public static void roota0() {

    int d1 = 20;
    int d2 = 15;
    int a = 7;
    int a1 = 6;
    int a5 = 1;
    int a3 = 0;
    int l1 = 0;
    int l2 = 1;
    int x1 = 0;
    int x2 = 5;
    int m1 = 0;
    int m2 = 5;
    int n1 = 0;
    int n2 = 5;

     for(int d = d1; d > d2; d = d - 1) {

          for(int x = x1; x < x2; x = x + 1) { 

              for(int l = l1; l < l2; l = l + 1) {

                  for(int m = m1; m < m2; m = m +1) {  

                      for(int n = n1; n < n2; n = n + 1) {

                          if(((Math.abs(msp[d-a3][l] - msp[d - a5][n])  == Math.abs(msp[d-a][x] - msp[d-a1][ m ])) | 
               (Math.abs(msp[d- a3 ][ l ] + msp[ d - a5 ][ n ]) == Math.abs(msp[d- a ][ x ] - msp[d- a1 ][ m ])) | 
               (Math.abs(msp[d- a3 ][ l ] - msp[ d - a5 ][ n ]) == Math.abs(msp[d- a ][ x ] + msp[d- a1 ][ m ])) | 
               (Math.abs(msp[d- a3 ][ l ] + msp[ d - a5 ][ n ]) == Math.abs(msp[d- a ][ x ] + msp[d- a1 ][ m ])))) {

         System.out.println(msp[d-a][x]);


                           }
                          }
                         }
                        }
                       }
                      }
                     }
                    }

The output is: 1 1 1 33 5 87 87 65 65 12 12 80 16 68 90 73 73 33 15 35 35 54

My problem is I don't want to print them but to convert them into a list. I rely on the net to learn. When it gets tough, I can only rely on expert on this platform. I'll be happy if someone can help me out.

1) Create a list outside of the loop:

private List<Integer> myList = new ArrayList()<>; 

2) Replace:

System.out.println(msp[d-a][x]);

With:

myList.add(msp[d-a][x]); 

Good luck!

myList.add(msp[da][x]); instead of System.out.println(msp[da][x]);

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