0
私はtoString()メソッドについて理解していますが、外部メソッドが呼び出されたときに戻り型でなければなりません。内容を1行ずつ表示するカスタムArrayList toString()を作成しますか?
下記のコメントブロックは、私が何をしようとしているのかを説明しています。
後で私がセッターとゲッターを使って作業するとき、この知識は非常に貴重です。
import java.util.*;
public class Display_ArrayList {
static ArrayList<String> cars = new ArrayList<String>();
public static void main(String[] args) {
cars.add("Nissan Maxima");
cars.add("Toyota Prius");
cars.add("Renault Clio");
cars.add("Ford Focus");
cars.add("Volkwagen Passat");
System.out.println("");
System.out.println("[Standard toString()]:");
System.out.println(cars.toString());
System.out.println("");
System.out.println("[Custom toString()]:");
System.out.println(custom_cars_toString());
}
// Array list displays the car list all on the same line
public static String getCarList() {
return cars.toString();
}
// *************************************************************************
// I want Array list contents to be displayed on their own lines without
// commas or brackets, while at the same allowing this method to be
// retrieved by a toString() method
// *************************************************************************
// public static void getCarList() {
// for (String element : cars)
// System.out.println(element);
// }
public static String custom_cars_toString() {
return "The cars contained are: \n" + getCarList();
}
}
これは機能しました!どうもありがとうございました。 – gimmegimme