[英]How output matrix with a single loop (java)
如何output使用单循环矩阵形式的二维数组的值(禁止使用Arrays class或第三方库的方法)。
int[][] data = {{1,2,3},{4,5,6,7,8},{10}};
output 以控制台为矩阵!
1 2 3
4 5 6 7 8
10
int[][] data = {{1, 2, 3}, {4, 5, 6, 7, 8}, {10}};
int currSubArrayNum = 0;
for (int i = 0; currSubArrayNum < data.length; i++) {
System.out.print(data[currSubArrayNum][i]+" ");
if (i == data[currSubArrayNum].length - 1) {
currSubArrayNum++;
i = -1;
System.out.println();
}
}
这就是您可以处理此任务的方式。
首先写下您通常用来执行此操作的循环:
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println();
}
现在我说可以重写这段代码,以便只有一个循环。 为了做到这一点,我将详细说明for
循环对变量的实际作用:
int i = 0;
while (i < data.length) {
int j = 0;
while (j < data[i].length) {
System.out.print(data[i][j] + " ");
j++;
}
System.out.println();
i++;
}
然后我将j
变量移出外循环:
int i = 0;
int j = 0;
while (i < data.length) {
while (j < data[i].length) {
System.out.print(data[i][j] + " ");
j++;
}
System.out.println();
i++;
j = 0;
}
最后,我决定代码在哪个循环中运行实际上并不重要,只要它在变量和循环...循环的相同条件下运行即可。
int i = 0;
int j = 0;
while (i < data.length) {
if (j < data[i].length) {
System.out.print(data[i][j] + " ");
j++;
} else {
System.out.println();
i++;
j = 0;
}
}
最后一步并不完全直截了当,但你不能否认与上一段的相似之处。
任何嵌套循环都可以使用这种技术展开。 您将所有变量移到外面并在这里和那里用if
s 替换while
s,瞧!
诀窍是从元素[x][y]开始,并且对于每一步,打印出(x,y)并递增y 。 如果y超出范围(即等于或大于[x].length
),则增加x并将y重置为 0(并打印出换行符)。
void matrix(int[][] array) {
int dim1 = 0;
int dim2 = 0;
while (dim1 < array.length) {
if (array[dim1].length > 0) {
System.out.print(array[dim1][dim2]);
System.out.print(" ");
}
dim2++; // Move to next element of sub-array
if (dim2 >= array[dim1].length) {
dim1++;
dim2 = 0;
System.out.println();
}
}
}
如果允许 2 个 for 循环,还可以在下一行打印每个子数组...
/** * */ package com.github.manishktiwari.matrix;
/**
*/ public class PrintMatrixInSingleLoop {
/**
* @param args
*/
public static void main(String[] args) {
int[][] mat = {{1,2,3},{4,5,6,7,8},{10}};
// Dimensions of the matrix
int N = mat.length;
int M = getWidth(mat);
// Function Call
print2DMatrix(mat, N, M);
}
private static int getWidth(int[][] mat) {
int width = -1;
for (int i = 0; i < mat.length; i++) {
int[] innerArray = mat[i];
if (width < innerArray.length) {
width = innerArray.length;
}
}
return width;
}
public static void print2DMatrix(
int arr[][], int rows, int columns)
{
// Iterate over the range
// [0, rows*columns]
for (int i = 0;
i < rows * columns; i++) {
// Find row and column index
int row = i / columns;
int col = i % columns;
try {
// Print the element
System.out.print(
arr[row][col] + " ");
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
System.out.println();
i = i + (columns-col-1);
}
if (col == columns-1) {
System.out.println();
}
}
}
}
有几种方法,这就是我的做法。
int[][] data = {{1,2,3},{4,5,6,7,8},{10}};
row
和col
被初始化为0
。row, col
、 value 并增加col
。col
到0
并增加row
。for (int row = 0, col = 0; row < data.length;) {
System.out.print(data[row][col++] + " ");
if (col == data[row].length) {
System.out.println();
col = 0;
row++;
}
}
印刷
1 2 3
4 5 6 7 8
10
重要的一点是在列被打印之前不要增加row
。
注意:如果处理空行很重要,请在 for 循环之后立即放置以下代码。 这将增加行并继续下一个。
if (data[row].length == 0) {
row++;
continue;
}
最常见的循环解决方案是 2 个循环(一个嵌套在另一个循环中),如下所示:
for (int i=0; i<data.length(); i++) {
for (int j=0; j<data[i].length(); i++) {
System.out.printf("%d ", data[i][j]);
System.out.println()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.