gpt4 book ai didi

java - 如何将 arraylist 与数组一起使用?

转载 作者:行者123 更新时间:2023-12-01 18:08:04 26 4
gpt4 key购买 nike

我让它遍历一个数组并从数组列表中为其赋值。我在尝试编写一个可以查看数组列表名称中的设置值是否有足够的行和列的东西时遇到了麻烦。每次打印时,它都只打印为一行,而不是打印为带有相应数字的行和列。我不知道我做错了什么。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

class SeatingChart {
public static void main(String[] args) {
// instanced variables
Scanner kb = new Scanner(System.in);
String name = "";
int r = 0;
int c = 0;
int k = 0;
String row = "";
String column = "";
ArrayList<String> names = new ArrayList<String>();
while (!name.equals("quit")) {
System.out.print("Enter first name of student: ");
// takes the name of kb and sets it to name
name = kb.nextLine();
// if the ArrayList names isEmpty then it adds the value at index
if (names.isEmpty()) {
names.add(name);
} else if (name.compareTo(names.get(names.size() - 1)) > 0) {
names.add(name);
} else {
// for loop going through the list of names to insert where it belongs
for (int i = 0; i < names.size(); i++) {
if (name.compareTo(names.get(i)) < 0) {
names.add(i, name);
i = names.size();
}
}
}
if (names.contains("quit")) {
names.remove("quit");
}
}
System.out.println(names);
// System.out.println(names);
System.out.println("how many rows do you have: ");
r = kb.nextInt();
System.out.println();
System.out.println("how many columns do you have: ");
c = kb.nextInt();
System.out.println();
System.out.println("Should students be seated row major(1) or column major(0)?");
if (kb.nextInt() == 1) {
// String arr[] = new String[names.size()];
String[][] chart = new String[r][c];
for (int i = 0; i < chart.length; i++) {
for (int j = 0; j < chart[0].length; j++) {
if (k < names.size()) {
chart[i][j] = names.get(k);
k += 1;
}
System.out.printf("%10s ", chart[i][j]);
}
}

} else if (kb.nextInt() == 2) {
String arr[] = new String[names.size()];
String[][] chart = new String[r][c];
for (int i = 0; i < names.size(); i++) {
arr[i] = names.get(i);
}
System.out.println(Arrays.toString(arr));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr[i] = chart[r][c];
k++;
}
}
for (int i = 0; i < chart[0].length; i++) {
for (int j = 0; j < chart.length; j++) {
System.out.printf("%10s ", chart[i][j]);
}
}
System.out.println("Column order!");
}
}
}

最佳答案

我已经修复了一个主要的IF block

    if (kb.nextInt() == 1) {
// String arr[] = new String[names.size()];
String[][] chart = new String[r][c];
for (int i = 0; i < chart.length; i++) {
for (int j = 0; j < chart[0].length; j++) {
if (k < names.size()) {
chart[i][j] = names.get(k);
k += 1;
}
System.out.printf("%10s ", chart[i][j]);
}
System.out.println(""); // ADD THIS
}

}

关于java - 如何将 arraylist 与数组一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60521069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com