我有一组代码,目前,使用的30个由数字手动inputed串的预先设定的列表,这在下面可以看到的数组:爪哇:创建随机串数
String[] elementsToAdd2 = { "100", "510", "170", "214", "268", "398",
"235", "802", "900", "723", "699", "1", "16", "999", "890",
"725", "998", "978", "988", "990", "989", "984", "320", "321",
"400", "415", "450", "50", "660", "624" };
我想用一些其他的东西替换它,这些东西可以在特定的预设大小之间产生任意数量的我想要的随机字符串数字。我脑子里想的是使用下面的代码在它的地方:
public class HashFunction {
.
.
.
private Random r = new Random();
public String randomString(int limit)
{
int n = r.nextInt(limit);
return n+"";
}
.
.
.
public static void main(String[] args) {
HashFunction theFunc = new HashFunction(30); // this is where you can control the number of spaces in the hash table!!!
String[] elementsToAdd2 = new String[30];
for (int i = 0; i <= elementsToAdd2.length; i++)
{
String[i] elementsToAdd2 = randomString(1000);
}
当我读到这,我把它意味着在阵列字符串值“i”将被分配到的随机值会介于0和1000之间。但是,我反而会收到一条错误消息。它说那个“;”预计,我有不兼容的类型。我认为Java正在读这个,因为我试图设置一个等于单个字符串的字符串数组,即使我在“String [i]”中包含了“i”来指定数组中要放置该值的位置。
如果有人能帮助我理解我的问题,请告诉我。谢谢!
p.s.此代码会创建哈希表,以防与我遇到的问题相关。如果您愿意,我可以提供代码,但我想分离问题并仅显示与其相关的内容。
不是'String [i] elementsToAdd2 = ...'但'elementsToAdd2 [i] = ...'。循环不应该在'i <= elementsToAdd2.length',而是'i
Marco13