简体   繁体   中英

Iterating through all slots that are not null, 2d array?

I must work with a 2d array. The maximum length of the row slots in the array is 100. More often than not, anywhere from 5-20 of these array slots will be filled and not more, however, I must build my code to a max of 100 rows. My question is, is there a way to iterate through only the array slots that have been set, stopping before the last unset, null slots?

//initialize array
String[][] variables = new String[numVariables][100];

//System.out.printf("%s \n", numVariables);

for(int i=0; i < numVariables; i++){
    //reads in variable line
    String variableLine = fin.nextLine();

    //turn variable line into array
    varArray = variableLine.split(" ");

    numRules = Integer.parseInt(varArray[0].replaceAll("\\s",""));

    for(int j=0; j < numRules+1; j++){
        variables[i][j] = varArray[j+1];

        System.out.printf("%s ", variables[i][j]);
    }
    System.out.println("\n");
}

//**LATER IN MY CODE ****//
//ITERATE THROUGH 'variables' array and PRINT OUT ONLY VALUES THAT ARE SET

If you populate the array in order from 0 to 100. If the first 51 elements are populated with the string then you could use:

    for(int i=0; i < numVariables; i++){
        for(int j=0; j < numRules+1; j++){
            if (variables[i][j] == null)
                break;
            System.out.printf("%s ", variables[i][j]);
        }
    }

Why do you even store the nulls if you don't need them? A String[][] is an array of arrays of String - those inner arrays need not have the same length. You could therefore create each inner array with the number of elements it needs:

           //initialize array
            String[][] variables = new String[numVariables][];

            //System.out.printf("%s \n", numVariables);

            for(int i=0; i < numVariables; i++){
                //reads in variable line
                String variableLine = fin.nextLine();

                //turn variable line into array
                varArray = variableLine.split(" ");

                numRules = Integer.parseInt(varArray[0].replaceAll("\\s",""));

                variables[i] = Arrays.copyOfRange(varArray, 1, numRules - 1);
            }

and then iterate:

for (String[] var : variables) {
    for (String s : var) {
        System.out.print(s);
    }
    System.out.println();
}

You can either use an array of List<String> or you can also keep track of the length of all your rows.

int [] rowLength = new int[numVariables]

for(int i=0; i < numVariables; i++){
    /* ... */

    numRules = Integer.parseInt(varArray[0].replaceAll("\\s",""));

    // store the number of slots in variable[i]
    rowLength[i] = numRules+1;

    /* ... */
}

Then you iterate only from zero to the length of your row ( rowLength[i] ).

A third approach (and the one I would prefer ) is to not specify the length of the row slots in your array :

String [][] variables = new String[numVariables][];

Then after having calculated numRules:

variables[i] = new String[numRules+1];

Given the situation you have described you'd probably be better off using another data structure, but seeing as you cannot you could keep a record of what values have been changed.

So you could keep an array of the length of each row and end each search there for the row.

    String[][] variables = new String[numVariables][100];
    int rowLength[] = new int[numVariables];

    //...
    // get and record the rowLength
    //...

    for(int x=0; x < numVariables; x ++) {
        for(int y=0; y < rowLength[x]; y ++) {
            // do your stuff
        }
    }
}

Or you could use a map or ArrayList to keep track of each of the positions that contain numbers.

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