【问题标题】:How can one use java statements to get indexes from string arrays of string arrays to print a statement?如何使用 java 语句从字符串数组的字符串数组中获取索引以打印语句?
【发布时间】:2020-12-29 10:26:56
【问题描述】:
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!");
    }
}

您好,我正在尝试使用字符串数组来打印位置中的随机目标,但我认为我还远远不够。

【问题讨论】:

  • 您是如何决定在 .get() 的结果上调用 .Random() 的??
  • Java 命名约定的方法和变量以小写字母开头。
  • @azro 我的逻辑是使用 .Random() 从该数组中随机选择一个条目。我的目标是打印朋友的名字以及随机地点和随机礼物
  • @NomadMaker 你是对的。我有点忘记了这一点,因为我正在处理字符串并想用一种方法来处理这一切。
  • 我知道了,但是为什么你认为你存在一个 .Random() 方法,因为它根本不存在

标签: java arrays string arraylist random


【解决方案1】:

您要查找的属性是“大小”。

ArrayList和数组在这方面是不同的。你用“arr.length”得到一个数组的长度,用“arrList.size()”得到一个ArrayList的长度(或大小)。

还可以使用 Random 类的 nextInt 方法生成大小范围内的随机索引。

下面是类似于您尝试执行的代码,它为每个朋友挑选随机礼物和地点。

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())));
     }
}

编辑:使用 Map 添加了一种更具可扩展性的方式,因为如果您继续增加属性,维护起来会很忙。

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));
    }
}

【讨论】:

  • 谢谢!我习惯使用 .length ,所以我倾向于忘记 .size ,尽管我想知道这种方法是否可以扩展或者是否可以使这段代码运行得更短。就像我添加了另一个数组列表一样,是否可以从主数组列表进行随机调用? ` ArrayList[] 目标 = new ArrayList(Arrays.asList(new String[] {地点、礼物、事件、人物、动物}); `
  • 就复杂性而言。从带有索引的数组列表中获取元素并获取列表的长度是恒定时间操作(O(1))。如果您想要可扩展性,我建议您可以使用 arraylist 的 HashMap,键是您要随机获取的属性,因为您可以在恒定时间内从“主”哈希图中获取任何列表。从我更新的代码中可以看出,您可以使用 keySet() 方法获取键列表并从每个列表中获取每个随机属性。这是一种更通用的方法。
猜你喜欢
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2012-11-09
  • 2018-07-06
  • 1970-01-01
  • 2016-04-30
  • 2021-12-27
相关资源
最近更新 更多