Java 如何以螺旋形式打印不等行长矩阵?
我有这样一个矩阵:Java 如何以螺旋形式打印不等行长矩阵?,java,arrays,algorithm,matrix,Java,Arrays,Algorithm,Matrix,我有这样一个矩阵: { {4,5,6,7,8}, {2,3,1,4}, {1,2}, {1,2,3,4,5} {5,6,7,8} } 我想以螺旋方式打印,所以输出应该是: 456784258761132 我目前拥有的螺旋矩阵代码如下: { {4,5,6,7,8}, {2,3,1,4}, {1,2}, {1,2,3,4,5} {5,6,7,8} } void spiral(inta[]]){ int i,k=0,l=0,m=a.长度,n=a[0].长度;
{
{4,5,6,7,8},
{2,3,1,4},
{1,2},
{1,2,3,4,5}
{5,6,7,8}
}
我想以螺旋方式打印,所以输出应该是:
456784258761132
我目前拥有的螺旋矩阵代码如下:
{
{4,5,6,7,8},
{2,3,1,4},
{1,2},
{1,2,3,4,5}
{5,6,7,8}
}
void spiral(inta[]]){
int i,k=0,l=0,m=a.长度,n=a[0].长度;
而(k=l;--i){
系统输出打印(a[m-1][i]+“”);
}
m--;
}
如果(l=k;--i){
系统输出打印(a[i][l]+“”);
}
l++;
}
}
问题是,我无法理解如何使其适用于不等数组长度的行。我非常感谢您在这方面提供的帮助和见解。以下是我的解决方案。它可能更有效,但它可以:
public static void main(String [] args) throws ScriptException {
Integer [][] input = new Integer[][] {
{4,5,6,7,8},
{2,3,1,4},
{1,2},
{1,2,3,4,5},
{5,6,7,8}
};
List<List<Integer>> list = listify(input);
processSpiral(list);
}
private static <T> void processSpiral(List<List<T>> list) {
if (!list.isEmpty()) {
printTop(list);
list.removeIf(l -> l.isEmpty());
printLast(list);
list.removeIf(l -> l.isEmpty());
printBottom(list);
list.removeIf(l -> l.isEmpty());
printFirst(list);
list.removeIf(l -> l.isEmpty());
processSpiral(list);
}
}
private static <T> void printFirst(List<List<T>> list) {
if (!list.isEmpty()) {
for (int i = list.size() -1; i >= 0; i --) {
List<T> row = list.get(i);
if (!row.isEmpty()) {
System.out.print(String.format("%s ", row.get(0).toString()));
row.remove(0);
}
}
}
}
private static <T> void printBottom(List<List<T>> list) {
if (!list.isEmpty()) {
List<T> bottomRow = list.get(list.size() - 1);
Collections.reverse(bottomRow);
for (T i : bottomRow) {
System.out.print(String.format("%s ", i.toString()));
}
list.remove(list.size() - 1);
}
}
private static <T> void printLast(List<List<T>> list) {
list.stream()
.filter(l -> !l.isEmpty())
.forEach(l -> {
T i = l.get(l.size() - 1);
System.out.print(String.format("%s ", i.toString()));
l.remove(l.size() - 1);
});
}
private static <T> void printTop(List<List<T>> list) {
List<T> firstRow = list.get(0);
list.remove(0);
//print the first row
for (T i : firstRow) {
System.out.print(String.format("%s ", i.toString()));
}
}
private static <T> List<List<T>> listify(T [][] input) {
//convert to List of List for simplicity's sake
List<List<T>> list = new ArrayList<>();
for (T [] row : input) {
//do not use Arrays.asList() as that creates an immutable List
List<T> l = new ArrayList<>(row.length);
for (T i : row) {
l.add(i);
}
list.add(l);
}
return list;
}
publicstaticvoidmain(字符串[]args)引发脚本异常{
整数[][]输入=新整数[][]{
{4,5,6,7,8},
{2,3,1,4},
{1,2},
{1,2,3,4,5},
{5,6,7,8}
};
列表=列表化(输入);
进程(列表);
}
私有静态void processSpiral(列表){
如果(!list.isEmpty()){
printTop(列表);
list.removif(l->l.isEmpty());
最后打印(列表);
list.removif(l->l.isEmpty());
打印底部(列表);
list.removif(l->l.isEmpty());
打印第一(列表);
list.removif(l->l.isEmpty());
进程(列表);
}
}
私有静态void printFirst(列表){
如果(!list.isEmpty()){
对于(int i=list.size()-1;i>=0;i--){
列表行=List.get(i);
如果(!row.isEmpty()){
System.out.print(String.format(“%s”,row.get(0.toString()));
行。删除(0);
}
}
}
}
私有静态void打印底部(列表){
如果(!list.isEmpty()){
List bottomRow=List.get(List.size()-1);
集合。反向(最下面一行);
对于(ti:最底层行){
System.out.print(String.format(“%s”,i.toString());
}
list.remove(list.size()-1);
}
}
私有静态void printLast(列表){
list.stream()
.filter(l->!l.isEmpty())
.forEach(l->{
ti=l.get(l.size()-1);
System.out.print(String.format(“%s”,i.toString());
l、 移除(l.尺寸()-1);
});
}
私有静态void打印顶部(列表){
List firstRow=List.get(0);
列表。删除(0);
//打印第一行
对于(T i:第一行){
System.out.print(String.format(“%s”,i.toString());
}
}
私有静态列表列表化(T[][]输入){
//为了简单起见,转换为列表列表
列表=新的ArrayList();
对于(T[]行:输入){
//不要使用Arrays.asList(),因为这样会创建不可变列表
列表l=新的ArrayList(行长度);
对于(TI:row){
l、 加(i);
}
列表。添加(l);
}
退货清单;
}
您的输出有四次出现1,而您的输入只有三次?Hm。您可以“消费”行,即在打印时从数组中删除元素:打印第一行并删除所有元素;打印该行下方所有行的最后一个元素并删除它们。从右到左打印最后一行;删除所有元素。打印仍具有自下而上元素的行的第一个元素。重复。当没有其他元素时停止打印。请不要仅将代码作为答案发布,而是提供一个解释,说明您的代码的作用以及它如何解决问题。带有解释的答案通常质量较高,并且更有可能吸引更多的选票。