Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NetBeans中的字符串和imag数组随机?_Java_String_Random_Netbeans 7 - Fatal编程技术网

Java NetBeans中的字符串和imag数组随机?

Java NetBeans中的字符串和imag数组随机?,java,string,random,netbeans-7,Java,String,Random,Netbeans 7,如何在NetBeans中生成字符串和图像数组?我试着让数组像 string{"lion","cat","dog","bird"}; 如何使用Random类获取s[I]其中I是随机的?字符串[]动物={“狮子”、“猫”、“狗”、“鸟”}; int rndindex=(int)(Math.random()*元素的个数); 字符串rndAnimal=动物[rndIndex]; 制作数组,得到一个介于0和n之间的随机数,其中n是数组中的元素数。然后检索该元素 如果我理解你的问题,一个解决办法就是使用

如何在NetBeans中生成字符串和图像数组?我试着让数组像

string{"lion","cat","dog","bird"};

如何使用Random类获取
s[I]
其中
I
是随机的?

字符串[]动物={“狮子”、“猫”、“狗”、“鸟”};
int rndindex=(int)(Math.random()*元素的个数);
字符串rndAnimal=动物[rndIndex];


制作数组,得到一个介于0和n之间的随机数,其中n是数组中的元素数。然后检索该元素

如果我理解你的问题,一个解决办法就是使用。我知道,你想用一个
String[]
来做这件事,但是我们可以使用这个
列表
是由一个
String[]
支持的,就像这样

java.util.Random rand = new java.util.Random();
int index = rand.nextInt(4);
System.out.println(string[index]);
public static void main(String[] args) {
  String[] animals = { "lion", "cat", "dog", "bird" };
  System.out.println(Arrays.toString(animals));
  Collections.shuffle(Arrays.asList(animals));
  System.out.println(Arrays.toString(animals));
}
输出为

[lion, cat, dog, bird]
[cat, lion, dog, bird]
编辑

如果您只想在数组中的一个随机位置放置一个元素,那么

String[] animals = { "lion", "cat", "dog", "bird" };
System.out.println(animals[rand.nextInt(animals.length)]);

String[]r={…}--他想要r[i]一个元素。一张照片来自照片数组,用户将在文本字段中输入名称。如何?@user3784250创建一个包含动物名称和照片的对象,然后使用该对象的数组。然后您可以使用我的第一个示例来洗牌您的数组。还有,什么文本字段?导入javax.swing.jtexfield@Eric编辑,如果你是对的。我们看不到你的设置——我们不知道图像是什么,它们在哪里,或者它们是什么格式。请提供有关图像/照片阵列的更多信息。我们愿意帮忙,但事实上,在黑暗中。