[英]Multiple List<String> in one line
I need to display multiple list arrays in one line through the for loop - so basically I need to count them as well.我需要通过 for 循环在一行中显示多个列表数组 - 所以基本上我也需要计算它们。
Example:例子:
Here is my code:这是我的代码:
List<String> name = new ArrayList<String>();
List<String> number = new ArrayList<String>();
name.add("Tommy"); // Test name
name.add("Tom"); // Test name 2
number.add(new String("123-456-7890")); // Test phone number
number.add(new String("098-765-4321")); // Test phone number 2
for (String d:name) {
System.out.print(d);
}
for (String b:number) {
System.out.println(b);
}
And here is my output with my code:这是我的代码输出:
Sorry if this question was duplicated, if it is, I will delete my question right away but for now I haven't fount anything like this.对不起,如果这个问题重复了,如果是,我会立即删除我的问题,但现在我还没有找到这样的东西。
Thank you.谢谢你。
I think in this case you are better off using a Map, which allows you more flexibility and better control over the entries:我认为在这种情况下,您最好使用 Map,它可以让您更加灵活并更好地控制条目:
import java.util.HashMap;
import java.util.Map;
/**
* Created by dlcol_000 on 3/19/2016.
*/
public class MapExample {
public void execute(){
Map<String,String> phoneDirectory = new HashMap<>();
phoneDirectory.put("Tommy","123-456-7890");
phoneDirectory.put("Tom", "098-765-4321");
//Now you can do things like, look up the phone number for a person:
String tomsPhone = phoneDirectory.get("Tom");
System.out.println("Toms' phone number is: " + tomsPhone);
//Or you can print the contents of the phone directory
System.out.println("The phone directory contains: " + phoneDirectory);
//Or you can iterate over all people in the directory printing their numbers
for(String person: phoneDirectory.keySet()){
System.out.println(person + ": " + phoneDirectory.get(person));
}
}
public static void main(String... args){
new MapExample().execute();
}
}
Which gives the following output:这给出了以下输出:
Toms' phone number is: 098-765-4321
The phone directory contains: {Tom=098-765-4321, Tommy=123-456-7890}
Tom: 098-765-4321
Tommy: 123-456-7890
You are printing complete the name list and then the number list, you should instead intercalate/alternate and print one from each list for every iteration您正在打印完整的名称列表,然后是数字列表,您应该插入/交替并在每次迭代时从每个列表中打印一个
final List<String> name = new ArrayList<String>();
final List<String> number = new ArrayList<String>();
name.add("Tommy"); // Test name
name.add("Tom"); // Test name 2
number.add(new String("123-456-7890")); // Test phone number
number.add(new String("098-765-4321")); // Test phone number 2
for (int i = 0; i < name.size(); i++) {
System.out.println(name.get(i) + "\t\t" + number.get(i));
}
at the end I will suggest you as comment above to define a Class " ContactPerson
" and override the toString
method.最后,我会建议你作为上面的评论来定义一个“
ContactPerson
”类并覆盖toString
方法。
With that approach you will get it faster and safer使用这种方法,您将获得更快、更安全
It really depends on what you want to use your objects for.这实际上取决于您想将对象用于什么目的。
One important aspect is that one should separate concerns.一个重要的方面是人们应该分离关注点。 In your case:
在你的情况下:
Based on that, you could the following for starters:基于此,您可以为初学者执行以下操作:
toString()
method that contains both name and phone numbertoString()
方法And all of a sudden, when iterate your list of Person objects and call System.out.println(person.toString()) for each person;突然之间,当迭代您的 Person 对象列表并为每个人调用 System.out.println(person.toString()) 时; you have nicely formatted output.
你有很好的格式化输出。
My key message to you: understand that there are plenty of choices;我给你的关键信息:明白有很多选择; and shouldn't stop on the first "working" version;
并且不应该停留在第一个“工作”版本上; but play around ...
但是玩...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.