package date_array;
import java.utils.Arrays;
class MyDate {
public int day;
public String month;
public int year;
public MyDate(String month, int day, int year)
{
this.day = day;
this.month = month;
this.year = year;
}
public String toString()
{
return (month + " " + day + " " + year);
}
/*public class DateArray {
MyDate[] dateArr = new MyDate[4];
dateArr[0] = new MyDate("May", 16,1984);
dateArr[1] = new MyDate("May", 16,1984);
dateArr[2] = new MyDate("May", 16,1984);
dateArr[3] = new MyDate("May", 16,1984);
}*/
// The above code does nothing
public static void main(String[] args) {
MyDate[] dateArr = new MyDate[4];
dateArr[0] = new MyDate("May", 16,1984); // add these to populate the array, otherwise it will be an array of nulls
dateArr[1] = new MyDate("May", 16,1984);
dateArr[2] = new MyDate("May", 16,1984);
dateArr[3] = new MyDate("May", 16,1984);
// dateArr.toString(); This does nothing, but would have also only given you the address of the array anyways and nothing useful
System.out.println(Arrays.toString(dateArr)); // print to the console using the Arrays.toString utility. This is a convenience method that iterates through the array and calls toString on each element and also gracefully handles null
}
}
如果你必须有 DateArray 类:
package date_array;
import java.utils.Arrays; // import the arrays library
class MyDate {
public int day;
public String month;
public int year;
public MyDate(String month, int day, int year)
{
this.day = day;
this.month = month;
this.year = year;
}
public String toString()
{
return (month + " " + day + " " + year);
}
public class DateArray {
private MyDate[] dateArr = new MyDate[4]; // package scoped member array, should probably be private
DateArray() { // move array initialization into constructor so that it is populated when the class is instantiated
dateArr[0] = new MyDate("May", 16,1984);
dateArr[1] = new MyDate("May", 16,1984);
dateArr[2] = new MyDate("May", 16,1984);
dateArr[3] = new MyDate("May", 16,1984);
}
public String toString() { // override the toString method so that when you just pass it to print it knows how to turn it into a string, otherwise it will just print the address of the object.
return Arrays.toString(dateArr);
}
}
public static void main(String[] args) {
DateArray dateArr = new DateArray();
System.out.println(dateArr); // You can call dateArr.toString, but this is done by the println method internally, so not necessary
}
}
此外,如果不允许您使用 Arrays.toString 方法,那么您必须自己实现一个 null 安全实现,如下所示:
public String toString() {
String toReturn = "[ "; // initialize the string that you will append to
for (int i = 0; i < dateArr.length; ++i) { // iterate through the array with i being the index
if (i != 0) // if it isn't the first one
toReturn += ", "; // insert a separator
if (dateArr[i] == null)
toReturn += "NULL"; // append the word null, because the array index is null
else
toReturn += dateArr[i]; // append the index's toString
}
toReturn += " ]"; // close off the array visualization
return toReturn;
}
注意: 上面的代码应该使用 StringBuilder,但为 OP 进行了简化。以这种方式附加字符串(使用 +=)将“污染”Java 字符串池。虽然优化器会尝试自动纠正此错误,但并不能保证。