how to print a 2d array in java
for (int row = 0; row < arr.length; row++)//Cycles through rows
{
for (int col = 0; col < arr[row].length; col++)//Cycles through columns
{
System.out.printf("%5d", arr[row][col]); //change the %5d to however much space you want
}
System.out.println(); //Makes a new row
}
//This allows you to print the array as matrix
print 2d array in java
int[][] array = new int[rows][columns];
System.out.println(Arrays.deepToString(array));
print 2d array
2-D Vectors
vector> vect;
for (int i = 0; i < vect.size(); i++)
{
for (int j = 0; j < vect[i].size(); j++)
{
cout << vect[i][j] << " ";
}
cout << endl;
}
java print 2d array as table
static void debugV2(Object... obj) {
System.out.println(Arrays.deepToString(obj)
.replace("],", "\n").replace(",", "\t")
.replaceAll("[\\[\\]]", " "));
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj).replace("], ", "]\n"));
}
printing 2d array in java
public class Sample {
public static void main(String[] args) {
String roles[][] = {
{ "admin", "customer", "cashier", "manager" },
{ "Jasmine", "lyka", "marbie", "soleen" },
{ "mama", "papa", "jenilyn", "efren" }
};
for (int i = 0; i < roles.length; i++) {
for (int j = 0; j < roles[i].length; j++) {
System.out.println(roles[i][j] + " ");
}
System.out.println("");
}
}
}
|