这是我的代码
ArrayList<Restaurant> restaurant= new ArrayList<Restaurant>();
餐厅内部课程,
@Override
public String toString() {
int i=1;
return "\n"+(i++)+". "+this.restaurantName +
"\t\t"+this.location;
}
我要这样打印
[1.班加罗尔披萨小屋,2.多米诺骨牌德里]
代替打印
[1.班加罗尔披萨小屋,1.多米诺骨牌德里]
需要帮助的代码。
答案 0 :(得分:2)
这里是另一种解决方案,我不确定您遇到了什么实际问题,因此请提供另一个可能的解决方案。这可能对您有用。
public class Restaurant {
static int index = 1;
String restaurantName;
String location;
int curIndex;
Restaurant(final String restaurantName, final String location) {
this.restaurantName = restaurantName;
this.location = location;
this.curIndex = index++;
}
public static void main(final String[] input) {
final ArrayList<Restaurant> restaurant = new ArrayList<Restaurant>();
restaurant.add(new Restaurant("pizzahut", "bangalore"));
restaurant.add(new Restaurant("dominos", "delhi"));
restaurant.forEach(r -> System.out.println(r));
}
public String toString() {
return "\n" + curIndex + ". " + this.restaurantName +
"\t\t" + this.location;
}
}
答案 1 :(得分:1)
ArrayList<Restaurant> restaurantList= new ArrayList<Restaurant>();
// your login to insert the elements in the array list
// iterate list
int index=1;
for(Restaurant r : restaurantList){
System.out.println(String.valueOf(index++)+": "+ r);
}
餐厅内部课程,
@Override
public String toString() {
return this.restaurantName + "\t\t"+this.location;
}