如何将分割的 ArrayList 显示在两列中?

huangapple go评论78阅读模式
英文:

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&lt;String&gt; teamA = Arrays.asList(
    &quot;Virgil van Dijk&quot;, &quot;Steven Gerrard&quot;, &quot;Edilson Cavani&quot;, &quot;Lionel Messi&quot;);
List&lt;String&gt; teamB = Arrays.asList(
    &quot;Paul Pogba&quot;,&quot;Neymar Jr&quot;,&quot;Roberto Firmino&quot;,&quot;Fernando Torres&quot;, &quot;Cristiano Ronaldo&quot;);

File teamFile = new File(&quot;teams.txt&quot;);

try (BufferedWriter writer = new BufferedWriter(new FileWriter(teamFile))) {
    for (int i = 0, n = Math.max(teamA.size(), teamB.size()); i &lt; n; i++) {
        String line = String.format(&quot;%-20s\t%-20s&quot;,
                        i &lt; teamA.size() ? teamA.get(i) : &quot;&quot;,
                        i &lt; teamB.size() ? teamB.get(i) : &quot;&quot;);
        writer.write(line.stripTrailing()));
        writer.write(&quot;\n&quot;);
    }
    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

huangapple
  • 本文由 发表于 2020年5月4日 21:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61594006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定