如何使用Java语句从字符串数组的字符串数组中获取索引以打印语句?

huangapple go评论62阅读模式
英文:

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&lt;&gt;(asList(&quot;Danny&quot;, &quot;Benni&quot;, &quot;Marcus&quot;, &quot;Pat&quot;));
	ArrayList Places = new ArrayList&lt;&gt;(asList(&quot;Paris&quot;, &quot;Brasil&quot;, &quot;Miami&quot;, &quot;Jamaica&quot;));
	ArrayList Gifts = new ArrayList&lt;&gt;(asList(&quot;Snacks&quot;, &quot;Photos&quot;, &quot;Instrument&quot;, &quot;Whine&quot;));
	
	ArrayList[] travelgoals = new ArrayList&lt;&gt;{Places, Gifts};
	
	for (int b = 0; b &gt; friends.length; b++){
	    if(b &gt; Places.length) {
	        System.out.println(Places.get().Random((0), Places.length);
	    }if (b &gt; Gifts.length) {
	        System.out.println(Gifts.get().Random((0), Gifts.length);
	    }
	}else {
	    System.out.println(&quot;Looks like you got off scottfree!&quot;);
	}
}

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&lt;String&gt; friends = new ArrayList&lt;&gt;(Arrays.asList(new String[]{&quot;Danny&quot;, &quot;Benni&quot;, &quot;Marcus&quot;, &quot;Pat&quot;}));
    ArrayList&lt;String&gt; places = new ArrayList&lt;&gt;(Arrays.asList(new String[]{&quot;Paris&quot;, &quot;Brasil&quot;, &quot;Miami&quot;, &quot;Jamaica&quot;}));
    ArrayList&lt;String&gt; gifts = new ArrayList&lt;&gt;(Arrays.asList(new String[]{&quot;Snacks&quot;, &quot;Photos&quot;, &quot;Instrument&quot;, &quot;Whine&quot;}));
        
    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&lt;String, ArrayList&lt;String&gt;&gt; mapOfProperties = new HashMap&lt;&gt;();

mapOfProperties.put(&quot;friends&quot;,Arrays.asList(new String[]{&quot;Danny&quot;, &quot;Benni&quot;, &quot;Marcus&quot;, &quot;Pat&quot;}));

mapOfProperties.put(&quot;places&quot;,Arrays.asList(Arrays.asList(new String[]{&quot;Paris&quot;, &quot;Brasil&quot;, &quot;Miami&quot;, &quot;Jamaica&quot;}));

mapOfProperties.put(&quot;gifts&quot;,Arrays.asList(new String[]{&quot;Snacks&quot;, &quot;Photos&quot;, &quot;Instrument&quot;, &quot;Whine&quot;}));

Set&lt;String&gt; keysOfMap = mapOfProperties.keySet(); 

Random rand = new Random();

for(int i=0;i&lt;map.get(&quot;friends&quot;).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));
    }
}

huangapple
  • 本文由 发表于 2020年9月11日 05:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63837884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定