如何使用java语句从字符串数组的字符串数组中获取索引以打印语句? publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{ ArrayList friends=新ArrayList(asList(“Danny”、“Benni”、“Marcus”、“Pat”)); ArrayList Places=新ArrayList(asList(“巴黎”、“巴西”、“迈阿密”、“牙买加”)); ArrayList礼品=新ArrayList(asList(“零食”、“照片”、“乐器”、“哀鸣”); ArrayList[]travelgoals=新的ArrayList{Places,Gifts}; 对于(int b=0;b>friends.length;b++){ 如果(b>位置长度){ System.out.println(Places.get().Random((0),Places.length); }如果(b>礼物长度){ System.out.println(Gifts.get().Random((0),Gifts.length); } }否则{ System.out.println(“看起来你从scottfree上下来了!”); } }

如何使用java语句从字符串数组的字符串数组中获取索引以打印语句? publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{ ArrayList friends=新ArrayList(asList(“Danny”、“Benni”、“Marcus”、“Pat”)); ArrayList Places=新ArrayList(asList(“巴黎”、“巴西”、“迈阿密”、“牙买加”)); ArrayList礼品=新ArrayList(asList(“零食”、“照片”、“乐器”、“哀鸣”); ArrayList[]travelgoals=新的ArrayList{Places,Gifts}; 对于(int b=0;b>friends.length;b++){ 如果(b>位置长度){ System.out.println(Places.get().Random((0),Places.length); }如果(b>礼物长度){ System.out.println(Gifts.get().Random((0),Gifts.length); } }否则{ System.out.println(“看起来你从scottfree上下来了!”); } },java,arrays,string,arraylist,random,Java,Arrays,String,Arraylist,Random,您好,我正在尝试使用字符串数组打印位置中的随机目标,尽管我认为我已经远远超出了这个范围。您要查找的属性是“大小” ArrayList和arrays在这方面有所不同。您可以使用“arr.length”获取数组的长度,使用“arrList.size()”获取ArrayList的长度(或大小) 还可以使用random类的nextInt方法在大小范围内生成随机索引 下面的代码与您尝试的代码类似,它为每个朋友随机挑选礼物和地点 public static void main (String[] args)

您好,我正在尝试使用字符串数组打印位置中的随机目标,尽管我认为我已经远远超出了这个范围。

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

ArrayList和arrays在这方面有所不同。您可以使用“arr.length”获取数组的长度,使用“arrList.size()”获取ArrayList的长度(或大小)

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

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

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!");
    }
}
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{
ArrayList friends=newArrayList(Arrays.asList(新字符串[]{“Danny”、“Benni”、“Marcus”、“Pat”}));
ArrayList places=newArrayList(Arrays.asList(新字符串[]{“Paris”、“Brasil”、“Miami”、“牙买加”}));
ArrayList gives=新的ArrayList(Arrays.asList(新字符串[]{“零食”、“照片”、“乐器”、“呜呜声”}));
Random rand=new Random();//这是可用于生成随机数的随机类。在本例中为整数
//只是一个遍历所有朋友的for循环
//这将从每个地方和礼物中随机选取一个元素
//您可以使用get命令获取列表的元素
//方法(arrList.get(index))
用于(字符串朋友:朋友){
System.out.println(places.get(rand.nextInt(places.size()));
System.out.println(gives.get(rand.nextInt(gives.size()));
}
}
编辑:添加了一种更具可伸缩性的使用地图的方法,因为如果你继续增加属性,它将忙于维护

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-mapOfProperties=newhashmap();
mapOfProperties.put(“friends”,Arrays.asList)(新字符串[]{“Danny”,“Benni”,“Marcus”,“Pat”});
mapOfProperties.put(“places”、Arrays.asList(Arrays.asList(新字符串[]{“Paris”、“Brasil”、“Miami”、“牙买加”);
mapOfProperties.put(“礼物”,数组.asList(新字符串[]{“零食”,“照片”,“乐器”,“呜呜声”}));
Set keysOfMap=mapOfProperties.keySet();
Random rand=新的Random();

对于(int i=0;i)您是如何决定对结果调用
.Random()
。get()?Java命名约定有以小写字母开头的方法和变量。@azro我的逻辑是使用.Random()从该数组中随机选择一个条目。我的目标是打印一个朋友的名字以及一个随机的位置和位置gift@NomadMaker你是对的。我有点忘记了,因为我在处理字符串,想用一个方法来处理这一切。我知道了,但你为什么认为你是一个.Random()方法存在,因为它不存在!我习惯于使用.length,所以我倾向于忘记.size,尽管我想知道这个方法是否可以扩展,或者是否可以使代码运行得更短。例如,如果我添加了另一个arraylist,那么是否可以从主arraylist进行随机调用?`arraylist[]objectives=newArrayList(Arrays.asList(new String[]{places,gifts,events,people,animals});`就复杂性而言,从带有索引的ArrayList中获取元素并获取列表的长度是一个常量时间操作(O(1))。如果您想要可伸缩性,我建议您可以使用arraylist的HashMap,其中键是您想要随机获取的属性,因为您可以在固定时间内从“主”HashMap中获取任何列表。如我更新的代码所示,您可以使用keySet()方法来获取键列表,并从每个列表中获取每个随机属性。这是一种更通用的方法。
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));
    }
}