Java Arraylist可打印具有计数的对象
这是我的密码Java Arraylist可打印具有计数的对象,java,Java,这是我的密码 ArrayList<Restaurant> restaurant= new ArrayList<Restaurant>(); 我想这样印刷 [1.班加罗尔比萨屋,2.德里多米诺骨牌] 而是打印 [1.班加罗尔比萨屋,1.德里多米诺骨牌] 需要代码方面的帮助。这里是另一个解决方案,我不确定您有什么实际问题,所以请提供另一个可能的解决方案。这可能对你有用 ArrayList<Restaurant> restaurantList= new Arra
ArrayList<Restaurant> restaurant= new ArrayList<Restaurant>();
我想这样印刷
[1.班加罗尔比萨屋,2.德里多米诺骨牌]
而是打印
[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);
}
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;
}
}
公共级餐厅{
静态int指数=1;
字符串餐厅名称;
字符串位置;
int-curIndex;
餐厅(最终字符串餐厅名称、最终字符串位置){
this.restaurantName=restaurantName;
这个位置=位置;
this.curIndex=index++;
}
公共静态void main(最终字符串[]输入){
最终ArrayList餐厅=新ArrayList();
新增(新餐厅(“比萨小屋”、“班加罗尔”);
添加(新餐厅(“多米诺骨牌”、“德里”);
restaurant.forEach(r->System.out.println(r));
}
公共字符串toString(){
返回“\n”+curIndex+”+this.restaurantName+
“\t\t”+此位置;
}
}
这里是另一个解决方案,我不确定您有什么实际问题,所以请提供另一个可能的解决方案。这可能对你有用
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;
}
}
公共级餐厅{
静态int指数=1;
字符串餐厅名称;
字符串位置;
int-curIndex;
餐厅(最终字符串餐厅名称、最终字符串位置){
this.restaurantName=restaurantName;
这个位置=位置;
this.curIndex=index++;
}
公共静态void main(最终字符串[]输入){
最终ArrayList餐厅=新ArrayList();
新增(新餐厅(“比萨小屋”、“班加罗尔”);
添加(新餐厅(“多米诺骨牌”、“德里”);
restaurant.forEach(r->System.out.println(r));
}
公共字符串toString(){
返回“\n”+curIndex+”+this.restaurantName+
“\t\t”+此位置;
}
}
使用静态int i=1作为类变量,并在迭代前初始化静态变量i=1list@RakeshSoni那是行不通的。我不认为这是一个重复的问题。但这是你试图解决的一个真正的问题,还是来自学校的一个旨在让你思考的问题(有故意的限制)?请在您的问题中说明答案,因为它会影响回答的方式。请使用静态int i=1作为类变量,并在迭代前初始化静态变量i=1list@RakeshSoni那是行不通的。我不认为这是一个重复的问题。但这是你试图解决的一个真正的问题,还是来自学校的一个旨在让你思考的问题(有故意的限制)?请在您的问题中说明答案,因为这会影响回答方式。打印与列表中的项目索引不相关的索引似乎有点。。。不寻常。虽然ctor分配避免了另一个(现已删除)答案的退化行为,但这仍然是一个坏主意TM。似乎打印与列表中的项目索引没有关联的索引有点。。。不寻常。虽然ctor赋值避免了另一个(现已删除)答案的退化行为,但这仍然是一个坏主意。
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;
}
}