打印 ArrayList 元素,一个接一个地。

huangapple 未分类评论45阅读模式
英文:

printing Arraylist elements one by one

问题

以下是您提供的代码的翻译部分:

有没有人知道如何逐个打印数组中的元素我有以下代码

    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class SimpleCollection {
    
        private String name;
        private ArrayList<String> elements;
    
        public SimpleCollection(String name) {
            this.name = name;
            this.elements = new ArrayList<>();
        }
    
        public void add(String element) {
            this.elements.add(element);
        }
    
        public ArrayList<String> getElements() {
            return this.elements;
        }
        
        public String toString(){
            String printOutput = "集合" + this.name + "有" + elements.size() + "个元素:";
            String e = "";
            if(elements.isEmpty()){
                return(printOutput + "为空。");
            }if(elements.size()==1){
                return "集合" + this.name + "有" + elements.size() + "个元素:" + "\n" + elements.get(0) + "\n";
                
            }
            
               e = printOutput + "\n" + getElements() + "\n";
          
            return e;
        }
            
    }

我写的toString方法将元素打印为数组我如何逐个打印它们类似于
集合characters有3个元素
magneto
mystique 
phoenix

请注意,代码中的HTML实体(例如&lt;&gt;)已被正确地翻译为普通的尖括号。

英文:

Does anyone here know how to print elements from the array one by one? I have this code here:

import java.util.ArrayList;
import java.util.Iterator;

public class SimpleCollection {

    private String name;
    private ArrayList&lt;String&gt; elements;

    public SimpleCollection(String name) {
        this.name = name;
        this.elements = new ArrayList&lt;&gt;();
    }

    public void add(String element) {
        this.elements.add(element);
    }

    public ArrayList&lt;String&gt; getElements() {
        return this.elements;
    }
    
    public String toString(){
        String printOutput = &quot;The collection &quot;+ this.name+ &quot; has &quot;+ elements.size()+ &quot; elements: &quot;;
        String e = &quot;&quot;;
        if(elements.isEmpty()){
            return(printOutput+ &quot; is empty.&quot;);
        }if(elements.size()==1){
            return &quot;The collection &quot;+ this.name+ &quot; has &quot;+ elements.size()+&quot; element:&quot;+ &quot;\n&quot;+ elements.get(0)+ &quot;\n&quot;;
            
        }
        
           e = printOutput + &quot;\n&quot;+ getElements() + &quot;\n&quot;;
      
        return e;
    }
        
}

The toString method of mine prints elements as an array: how can i print them one by one? Something like:
The collection characters has 3 elements:
magneto
mystique
phoenix

答案1

得分: 0

为了以你提供的示例方式获取输出(magneto mystique phoenix),一个快速的方法是:

String.join(" ", getElements())

希望有所帮助。

英文:

To get an output in the way you gave for an example (magneto mystique phoenix) one quick way would be

String.join(&quot; &quot;, getElements())

HTH

huangapple
  • 本文由 发表于 2020年4月6日 20:18:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/61059684.html
匿名

发表评论

匿名网友

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

确定