【发布时间】:2021-09-24 04:00:09
【问题描述】:
我正在尝试显示数组列表中的所有字符串内容,但数组列表的大小未知(使用 Android Studio) 比如:
fruit = new ArrayList<>()
veg = new ArrayList<>()
经过一些操作,现在故事数组列表中包含了一些信息,例如:
fruit = {"apple", "orange", "banana", "peach",...};
veg = {"cucumber", "spinach", "pumpkin", "broccoli",...};
我不知道水果和蔬菜的数组列表在操作后多久了。但我知道水果和蔬菜的数组列表大小相同。我想将每个列表存储为:
fruitOne = fruit(0), fruitTwo = fruit(1), fruitThree = fruit(2),...
vegOne = veg(0), vegTwo = veg(1), vegThree = veg(2),...
那我想把它们一起显示为一个字符串?这样我就可以有一个字符串,例如:
String myStore = "I am selling" + fruitOne + " and " + vegOne + "/n" + fruitTwo + " and " + vegTwo + "/n" + fruitThree + " and " + vegThree"...;
我觉得它需要使用 For 循环通过调用 fruit(0), fruit(1),...,fruit(i) 来一一拉出每个列表。但是如何独立存储fruit(0), fruit(1),...,fruit(i) 列表中的每一个,以便将它们连接在一起成为 myStore 的一个字符串?
所有教程都在讨论 printLn 或 logd,因此它可以打印每个 for 循环的 fruit(i) 或 veg(i),但不能真正将 fruit(i) 或 veg(i) 作为变量存储到可以独立使用。
感谢您的帮助!
【问题讨论】:
-
String.join(" ", story)怎么样?这样,您可以保持ArrayList原样并连接其条目。 -
你知道流吗?字符串生成器?字符串连接?你试过什么?您到底想做什么,记录所有内容,或将其全部存储在一个字符串中?
-
这只是一个例子,实际列表中更像是“apple”、“orange”、“peach”。然后我需要将连接的字符串变成像 = "我喜欢吃" + 水果 (0) + " 但我更喜欢吃" + 水果 (1) + ",我最喜欢的是 " + 水果 (2)。
-
我需要先将列表一个一个的存储在数组中,以便随时使用。例如,fruitOne = fruit(0)、fruitTwo =fruit(1)、fruitThree =fruit(2) 等。我还有另一个蔬菜列表是 vegOne = veg(0)、vegTwo = veg(1)、vegThree = veg(2) 等。稍后,我想要一个字符串 = "我在卖" + fruitOne + " and " + vegOne + "/n" +fruitTwo + " and " + vegTwo + "/n" +fruitThree + " 和 " + vegThree,这个列表一直持续到我在水果和蔬菜的数组列表中没有数据为止。
标签: java android arrays android-studio arraylist