Asked by Osprey
I am in an Intro to Java class. We were instructed to create a code using multidimensional arrays that would produce the output:
[5, 6, 7, 8]
[
[2, 4, 6, 8]
[8, 7, 9, 1]
[3, 5, 1, 2]
]
[
[1, 2]
[3, 4, 5]
[6]
[7, 8, 9]
]
I’ve been working on it, but have gotten stuck. If anyone could finish my code, and point out where I’m going wrong, I would greatly appreciate it. Here’s my code:
public class ArrayPrinter{
public static void main(String[]args) {
int[] oneD = {5, 6, 7, 8};
printArray(oneD);
System.out.println();
int[][] twoD = {{2, 4, 6, 8},
{8, 7, 9, 1},
{3, 5, 1, 2}};
printArray(twoD);
System.out.println();
int[][] twoD2 = {{1, 2},
{3, 4, 5},
{6},
{7, 8, 9}};
printArray(twoD2);
}
public static void printArray(int[] arr) {
System.out.print("[");
for(int i = 0; i < arr.length; i++) {
System.out.print(arr.length);
if(i < arr.length -1) {
System.out.print(" ");
}}
}
public static void printArray (int[][] arr) {
for (int i=0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void printArray(int[][][] arr) {
System.out.println(" ");
System.out.println("]");
printArray(arr[i]);
System.out.println("]");
System.out.println("]");
}}
[5, 6, 7, 8]
[
[2, 4, 6, 8]
[8, 7, 9, 1]
[3, 5, 1, 2]
]
[
[1, 2]
[3, 4, 5]
[6]
[7, 8, 9]
]
I’ve been working on it, but have gotten stuck. If anyone could finish my code, and point out where I’m going wrong, I would greatly appreciate it. Here’s my code:
public class ArrayPrinter{
public static void main(String[]args) {
int[] oneD = {5, 6, 7, 8};
printArray(oneD);
System.out.println();
int[][] twoD = {{2, 4, 6, 8},
{8, 7, 9, 1},
{3, 5, 1, 2}};
printArray(twoD);
System.out.println();
int[][] twoD2 = {{1, 2},
{3, 4, 5},
{6},
{7, 8, 9}};
printArray(twoD2);
}
public static void printArray(int[] arr) {
System.out.print("[");
for(int i = 0; i < arr.length; i++) {
System.out.print(arr.length);
if(i < arr.length -1) {
System.out.print(" ");
}}
}
public static void printArray (int[][] arr) {
for (int i=0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void printArray(int[][][] arr) {
System.out.println(" ");
System.out.println("]");
printArray(arr[i]);
System.out.println("]");
System.out.println("]");
}}
Answers
There are no human answers yet.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.