1

Need to store the incoming arrays as a single two dimensional array.

public class Question {

    static int row = 5;
    static int column = 3;
    static int[][] processedArrayData;

    public static void processArrays(int[] incoming) {
        processedArrayData = new int[column][row];

        /*
         * Need to store each incoming array as 
         * a single two dimensional array
         */

        // This attempt gives me invalid data See "Invalid Example Output 1"
          for (int i = 0; i < column; i++) {
                for (int j = 0; j < row; j++) {
                    processedArrayData[i][j] = incoming[i];
                }
            }
            for (int[] arr : processedArrayData) {
                System.out.println(Arrays.toString(arr));
            }




    }

    public static void main(String[] args) {
        int[] array1 = {7, 7, 1, 3, 3};
        int[] array2 = {9, 7, 0, 3, 8};
        int[] array3 = {6, 6, 1, 3, 2};

        processArrays(array1);
        processArrays(array2);
        processArrays(array3);
    }

}

Invalid Example Output 1

[7, 7, 7, 7, 7]
[7, 7, 7, 7, 7]
[1, 1, 1, 1, 1]
[9, 9, 9, 9, 9]
[7, 7, 7, 7, 7]
[0, 0, 0, 0, 0]
[6, 6, 6, 6, 6]
[6, 6, 6, 6, 6]
[1, 1, 1, 1, 1]

Every attempt I try to do so results in an error. I am assuming is it I am trying traditional methods of storing the array to the 2d array and all the array information is known and accessible before hand. When I try to do so, only calling in one array at a time, I can not get it to work.

2
  • maybe because you never initialize your 2d array Commented Nov 16, 2016 at 14:55
  • What is the error? Where is the code to store the array? Commented Nov 16, 2016 at 14:56

2 Answers 2

3

One option is to have your processArray() method simply add a new row to the two dimensional array using the input array. Then, maintain an index into the 2D array to keep track of where you are.

public class Question {

    private static int[][] processedArrayData;
    private static int index = 0;

    public static void processArrays(int[] incoming) {
        //for (int i=0; i < incoming.length; ++i) {
        //    processedArrayData[index][i] = incoming[i];
        //}
        // edit by @FelixNovovic
        processedArrayData[index] = incoming

        ++index;
    }

    public static void main(String[] args) {
        int[] array1 = {7, 7, 1, 3, 3};
        int[] array2 = {9, 7, 0, 3, 8};
        int[] array3 = {6, 6, 1, 3, 2};
        processedArrayData = new int[3][5];

        processArrays(array1);
        processArrays(array2);
        processArrays(array3);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

processedArrayData needs to be static, or a new Question instance needs to be created.
processedArrayData and index needs to be static for the code to compile. Additionally, you do not need the for loop, you only need to assign the incoming array to the position at index (processedArrayData[index] = incoming)
Thank you for your assistance. I do appreciate the time and effort.
Um, following Felix's comment, you've put processedArrayData[index] = incoming; inside a loop :-/
2

You would need to create an instance of class Question or make the variables also static.

static int[][] processedArrayData = new int[3][];
static int idx = 0;

public static void processArrays(int[] incoming) {
    processedArrayData[ idx++ ] = incoming;
}

2 Comments

Nice and compact use of the increment operator, +1
Thank you. This is exactly what I needed. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.