英文:
How do I display split ArrayList into 2 different columns?
问题
这是显示文本的部分。
A = jTextArea2.getText();
B = jTextArea3.getText();
Display = A + " " + B + " ";
PrintWriter pwFile = null;
fc = new JFileChooser();
int returnVal = fc.showSaveDialog(NewJFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String extension = fc.getSelectedFile().getAbsolutePath() + ".txt";
File file = new File(extension);
try {
pwFile = new PrintWriter(file);
pwFile.write(Receipt() + Display);
JOptionPane.showMessageDialog(null, "Save successful", "Reminder", 1);
pwFile.flush();
}
catch (FileNotFoundException e)
这是正确的显示方式。
如何将拆分的ArrayList显示在2个不同的列中?
英文:
This is too display the text.
A = jTextArea2.getText();
B = jTextArea3.getText();
Display = A+" " + B + " ";
PrintWriter pwFile = null;
fc = new JFileChooser();
int returnVal = fc.showSaveDialog(NewJFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String extension = fc.getSelectedFile().getAbsolutePath() + ".txt";
File file = new File(extension);
try {
pwFile = new PrintWriter(file);
pwFile.write(Receipt()+Display);
JOptionPane.showMessageDialog(null, "Save successful", "Reminder", 1);
pwFile.flush();
}
catch (FileNotFoundException e)
This how it suppose to display
How do I display split ArrayList into 2 different columns?
答案1
得分: 1
可以这样做:
List<String> teamA = Arrays.asList(
"Virgil van Dijk", "Steven Gerrard", "Edilson Cavani", "Lionel Messi");
List<String> teamB = Arrays.asList(
"Paul Pogba", "Neymar Jr", "Roberto Firmino", "Fernando Torres", "Cristiano Ronaldo");
File teamFile = new File("teams.txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(teamFile))) {
for (int i = 0, n = Math.max(teamA.size(), teamB.size()); i < n; i++) {
String line = String.format("%-20s\t%-20s",
i < teamA.size() ? teamA.get(i) : "",
i < teamB.size() ? teamB.get(i) : "");
writer.write(line.stripTrailing());
writer.write("\n");
}
writer.flush();
}
文件输出(仅球员):
Virgil van Dijk Paul Pogba
Steven Gerrard Neymar Jr
Edilson Cavani Roberto Firmino
Lionel Messi Fernando Torres
Cristiano Ronaldo
英文:
This can be done this way:
List<String> teamA = Arrays.asList(
"Virgil van Dijk", "Steven Gerrard", "Edilson Cavani", "Lionel Messi");
List<String> teamB = Arrays.asList(
"Paul Pogba","Neymar Jr","Roberto Firmino","Fernando Torres", "Cristiano Ronaldo");
File teamFile = new File("teams.txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(teamFile))) {
for (int i = 0, n = Math.max(teamA.size(), teamB.size()); i < n; i++) {
String line = String.format("%-20s\t%-20s",
i < teamA.size() ? teamA.get(i) : "",
i < teamB.size() ? teamB.get(i) : "");
writer.write(line.stripTrailing()));
writer.write("\n");
}
writer.flush();
}
File output (players only):
Virgil van Dijk Paul Pogba
Steven Gerrard Neymar Jr
Edilson Cavani Roberto Firmino
Lionel Messi Fernando Torres
Cristiano Ronaldo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论