简体   繁体   English

如何在java中使用for循环打印所有数组

[英]how to print all of arrays with for loop in java

I have array in String . 我在String有数组。 I want to use for loop to print all of the objects. 我想使用for循环来打印所有对象。 I was under the impression this would be done by making for loop then returning the String . 我的印象是,可以通过进行for循环然后返回String来完成。 I am not sure what I need to change to accomplish this. 我不确定要完成此操作需要更改什么。 Following is my effort: 以下是我的努力:

    public class SolarSystem {
        private Planet[] planets;
        private int position = 0;
     public SolarSystem(int size) {
         planets = new Planet[size];
     }
    public void add(Planet planet) {
        planets[position] = planet;
        position++; 
    }
    public String toString(){
          for(int i = 0; i < planets.length; i++){

          } 
          return toString();  
    }
   }

ADDED PLANET CLASS 新增PLANET班

public class Planet  {


    String name;
    int moons;

    public Planet(String name, int moons)
    {
        this.moons = moons;
        this.name = name;               
    }

    public String toString() {
        return "The Planet " + name  + " Has " + moons + " Moon(s) \r\n ";
    }

}

You can try using a StringBuilder : 您可以尝试使用StringBuilder

public String toString() {
    StringBuilder sb = new StringBuilder();

    for(int i = 0; i < planets.length; i++){
        sb.append(planets[i].getName()); // getName() or toString()
        sb.append("\n");
    }

    return sb.toString();
}

Override toString() method in Planet class, and use below code : 覆盖Planet类中的toString()方法,并使用以下代码:

public String toString(){
      String result = "";
      for(int i = 0; i < planets.length; i++){
          result += planets[i].toString(); // append comma if you need to separate it,
                          // and most of all handle nulls
      } 
      return result;  
}
public String toString() {
StringBuilder mainresult = new StringBuilder();
for(int i = 0; i < planets.length; i++){
  StringBuilder result = new StringBuilder();
  String newLine = System.getProperty("line.separator");

  result.append( planets[i].getClass().getName() );
  result.append( " Object {" );
  result.append(newLine);

  //determine fields declared in this class only (no fields of superclass)
  Field[] fields = this.getClass().getDeclaredFields();

  //print field names paired with their values
  for ( Field field : fields  ) {
    result.append("  ");
    try {
      result.append( field.getName() );
      result.append(": ");
      //requires access to private field:
      result.append( field.get(this) );
    } catch ( IllegalAccessException ex ) {
      System.out.println(ex);
    }
    result.append(newLine);
  }
  result.append("}");
  //if it is the first one then no new line added
  if(i==0)
  {
     mainresult.append(result.toString());
     continue;
  }
  mainresult.append(newLine);
  mainresult.append(result.toString());
}
  return mainresult.toString();
}

If you have this overidden method in your planet class 如果您的行星班级中有这种隐藏的方法

public String toString(){
    StringBuilder result = new StringBuilder();

    String NEW_LINE = System.getProperty("line.separator");

    result.append(" planetProperty1: ").append (planetProperty1).append(NEW_LINE);   
    result.append(" planetProperty2: ").append (planetProperty2).append(NEW_LINE);   

    return result.toString();
}

then from the calling class you can iterate over the collection calling 然后从调用类中可以遍历集合调用

  System.out.println (planets[i].toString());

Override toString() Method 重写toString()方法

@Override
public String toString() {
    return "SolarSystem [te=" + Arrays.toString(te) + ", position=" + position + "]";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM