英文:
How can one use java statements to get indexes from string arrays of string arrays to print a statement?
问题
public static void main (String[] args) throws java.lang.Exception {
ArrayList<String> friends = new ArrayList<>(Arrays.asList("Danny", "Benni", "Marcus", "Pat"));
ArrayList<String> Places = new ArrayList<>(Arrays.asList("Paris", "Brasil", "Miami", "Jamaica"));
ArrayList<String> Gifts = new ArrayList<>(Arrays.asList("Snacks", "Photos", "Instrument", "Whine"));
ArrayList[] travelgoals = new ArrayList[]{Places, Gifts};
for (int b = 0; b < friends.size(); b++) {
if (b < Places.size()) {
System.out.println(Places.get(new Random().nextInt(Places.size())));
}
if (b < Gifts.size()) {
System.out.println(Gifts.get(new Random().nextInt(Gifts.size())));
} else {
System.out.println("Looks like you got off scot-free!");
}
}
}
英文:
public static void main (String[] args) throws java.lang.Exception{
ArrayList friends = new ArrayList<>(asList("Danny", "Benni", "Marcus", "Pat"));
ArrayList Places = new ArrayList<>(asList("Paris", "Brasil", "Miami", "Jamaica"));
ArrayList Gifts = new ArrayList<>(asList("Snacks", "Photos", "Instrument", "Whine"));
ArrayList[] travelgoals = new ArrayList<>{Places, Gifts};
for (int b = 0; b > friends.length; b++){
if(b > Places.length) {
System.out.println(Places.get().Random((0), Places.length);
}if (b > Gifts.length) {
System.out.println(Gifts.get().Random((0), Gifts.length);
}
}else {
System.out.println("Looks like you got off scottfree!");
}
}
Hello I'm trying to use string arrays to print random goals in locations though I think I'm more than way off.
答案1
得分: 1
以下是翻译好的内容:
public static void main(String[] args) throws java.lang.Exception {
ArrayList<String> friends = new ArrayList<>(Arrays.asList(new String[]{"Danny", "Benni", "Marcus", "Pat"}));
ArrayList<String> places = new ArrayList<>(Arrays.asList(new String[]{"Paris", "Brasil", "Miami", "Jamaica"}));
ArrayList<String> gifts = new ArrayList<>(Arrays.asList(new String[]{"Snacks", "Photos", "Instrument", "Whine"}));
Random rand = new Random(); //这是用于生成随机数的Random类。在这种情况下为整数
//只是一个循环遍历所有朋友
//这将从每个地点和礼物中选择一个随机元素
//您可以使用get方法(arrList.get(index))获取列表的元素
for (String friend : friends) {
System.out.println(places.get(rand.nextInt(places.size())));
System.out.println(gifts.get(rand.nextInt(gifts.size())));
}
}
编辑:添加了一种更可扩展的使用Map的方式,因为如果不断增加属性,维护起来会很麻烦。
Map<String, ArrayList<String>> mapOfProperties = new HashMap<>();
mapOfProperties.put("friends", Arrays.asList(new String[]{"Danny", "Benni", "Marcus", "Pat"}));
mapOfProperties.put("places", Arrays.asList(new String[]{"Paris", "Brasil", "Miami", "Jamaica"}));
mapOfProperties.put("gifts", Arrays.asList(new String[]{"Snacks", "Photos", "Instrument", "Whine"}));
Set<String> keysOfMap = mapOfProperties.keySet();
Random rand = new Random();
for (int i = 0; i < mapOfProperties.get("friends").size(); i++) {
for (String keyName : keysOfMap) {
int keyIndex = rand.nextInt(mapOfProperties.get(keyName).size());
//也可以将mapOfProperties.get(keyName)放入一个临时ArrayList中
System.out.print(mapOfProperties.get(keyName).get(keyIndex));
}
}
英文:
The property you're looking for is "size".
ArrayList and arrays are different here in this aspect.You get the length of an array with "arr.length" and the length(or size) of an ArrayList with "arrList.size()".
Also to generate random index within the range of size you can use Random class's nextInt method.
Below is the code similar to what you're trying to do which picks random gifts and places for each friend.
public static void main (String[] args) throws java.lang.Exception{
ArrayList<String> friends = new ArrayList<>(Arrays.asList(new String[]{"Danny", "Benni", "Marcus", "Pat"}));
ArrayList<String> places = new ArrayList<>(Arrays.asList(new String[]{"Paris", "Brasil", "Miami", "Jamaica"}));
ArrayList<String> gifts = new ArrayList<>(Arrays.asList(new String[]{"Snacks", "Photos", "Instrument", "Whine"}));
Random rand = new Random();//This is the Random class that can be used to generate random number. In this case Integers
//just a for loop iterating through all friends
//This will pick a random element from each of places and gifts
//You can get an element of a list by using the get
//method(arrList.get(index))
for(String friend : friends){
System.out.println(places.get(rand.nextInt(places.size())));
System.out.println(gifts.get(rand.nextInt(gifts.size())));
}
}
Edit : Added a more scalable way using Map since if you keep on increasing properties it'll be hectic to maintain.
Map<String, ArrayList<String>> mapOfProperties = new HashMap<>();
mapOfProperties.put("friends",Arrays.asList(new String[]{"Danny", "Benni", "Marcus", "Pat"}));
mapOfProperties.put("places",Arrays.asList(Arrays.asList(new String[]{"Paris", "Brasil", "Miami", "Jamaica"}));
mapOfProperties.put("gifts",Arrays.asList(new String[]{"Snacks", "Photos", "Instrument", "Whine"}));
Set<String> keysOfMap = mapOfProperties.keySet();
Random rand = new Random();
for(int i=0;i<map.get("friends").size();i++){
for(String keyName : keysOfMap){
int keyIndex = rand.nextInt(mapOfProperties.get(keyName).size());
//can put mapOfProperties.get(keyName) in a temp arrayList as well
System.out.print(mapOfProperties.get(keyName).get(keyIndex));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论