Specify max and min for Random.nextInt()?

Possible Duplicate: Java: generating random number in a range I want to generate a random int in a logical range. So, say for example, I'm writing a program to "roll" a dice with a specified number of sides. public int rollDice() { Random generator = new Random(); return generator.nextInt(sides); } Now the problem becomes that this will return values between sides an

为Random.nextInt()指定max和min?

可能重复: Java:在范围内生成随机数 我想在逻辑范围内生成一个随机的int。 所以,比如说,我正在编写一个程序来“掷骰”一个指定数量的棋子。 public int rollDice() { Random generator = new Random(); return generator.nextInt(sides); } 现在问题变成了这将返回两侧和零之间的值,包括在内,这是没有意义的,因为大多数骰子从1到6,9等等。那么我怎样才能指定nextInt应该在1和边数之间工作? 要在from和to

Get random integer in range (x, y]?

Possible Duplicate: Java: generating random number in a range How do I generate a random integer i , such that i belongs to (0,10] ? I tried to use this: Random generator = new Random(); int i = generator.nextInt(10); but it gives me values between [0,10) . But in my case I need them to be (0,10] . Random generator = new Random(); int i = generator.nextInt(10) + 1; 怎么样: Random gen

获取范围(x,y)中的随机整数?

可能重复: Java:在范围内生成随机数 我如何生成一个随机整数i ,使i属于(0,10] ? 我试图使用这个: Random generator = new Random(); int i = generator.nextInt(10); 但它给了我[0,10)之间的值。 但在我的情况下,我需要他们(0,10] 。 Random generator = new Random(); int i = generator.nextInt(10) + 1; 怎么样: Random generator = new Random(); int i = 10 - generator.nextInt(10); 只需添加一个结果

Java random number with given length

This question already has an answer here: How do I generate random integers within a specific range in Java? 57 answers Generate a number in the range from 100000 to 999999 . // pseudo code int n = 100000 + random_float() * 900000; I'm pretty sure you have already read the documentation for eg Random and can figure out the rest yourself. To generate a 6-digit number: Use Random and

给定长度的Java随机数

这个问题在这里已经有了答案: 如何在Java中的特定范围内生成随机整数? 57个答案 生成范围在100000到999999 。 // pseudo code int n = 100000 + random_float() * 900000; 我很肯定你已经阅读了Random的文档,并且可以自己找出其余的。 要生成一个6位数字: 使用Random和nextInt如下: Random rnd = new Random(); int n = 100000 + rnd.nextInt(900000); 请注意, n永远不会是7位数(1000000),因为nextInt(900

Generating a Random Number between 1 and 10 Java

This question already has an answer here: How do I generate random integers within a specific range in Java? 57 answers

生成1到10个Java之间的随机数

这个问题在这里已经有了答案: 如何在Java中的特定范围内生成随机整数? 57个答案

Java Generate Random Number Between Two Given Values

This question already has an answer here: How do I generate random integers within a specific range in Java? 57 answers You could use eg r.nextInt(101) For a more generic "in between two numbers" use: Random r = new Random(); int Low = 10; int High = 100; int Result = r.nextInt(High-Low) + Low; This gives you a random number in between 10 (inclusive) and 100 (exclusive) 假设上

Java在两个给定值之间生成随机数

这个问题在这里已经有了答案: 如何在Java中的特定范围内生成随机整数? 57个答案 你可以使用例如r.nextInt(101) 对于更通用的“在两个数字之间”使用: Random r = new Random(); int Low = 10; int High = 100; int Result = r.nextInt(High-Low) + Low; 这给你一个在10(含)和100(不含)之间的随机数字, 假设上限是上限,下限是下限,则可以在两个边界之间使用随机数r: int r = (int) (Math.random() * (upper - l

How to convert an array into an ArrayList?

This question already has an answer here: Create ArrayList from array 32 answers 您可以简单地使用Arrays.asList(array)来创建Arrays.asList(array)的ArrayList 。

如何将数组转换为ArrayList?

这个问题在这里已经有了答案: 从数组32中创建ArrayList答案 您可以简单地使用Arrays.asList(array)来创建Arrays.asList(array)的ArrayList 。

Array of Strings into an ArrayList of Arraylist

This question already has an answer here: Create ArrayList from array 32 answers public static void main(String[] args) { String[] sentences = {"hello", "how are you", "i am fine", "and you ?", "thank you"}; System.out.println(split(2,sentences)); System.out.println(split(3,sentences)); } public static List<List<String>> split(int numberOfElements, String[] sentences)

Arraylist的ArrayList中的字符串数组

这个问题在这里已经有了答案: 从数组32中创建ArrayList答案 public static void main(String[] args) { String[] sentences = {"hello", "how are you", "i am fine", "and you ?", "thank you"}; System.out.println(split(2,sentences)); System.out.println(split(3,sentences)); } public static List<List<String>> split(int numberOfElements, String[] sentences) { List<List<St

Converting an array into Array List

This question already has an answer here: Create ArrayList from array 32 answers Your commented code is perfectly valid for initiliazing the arrayList. ArrayList<Property> property_list = new ArrayList<Property>(); In java 7 or later you don't need to specify the second Property: ArrayList<Property> property_list = new ArrayList<>(); Unlike the java Array you d

将数组转换为数组列表

这个问题在这里已经有了答案: 从数组32中创建ArrayList答案 您的评论代码对于启动arrayList非常有效。 ArrayList<Property> property_list = new ArrayList<Property>(); 在Java 7或更高版本中,您不需要指定第二个Property: ArrayList<Property> property_list = new ArrayList<>(); 与java数组不同,您不使用括号表示法来访问使用.get(int index)的变量: ppty.property_list.get(count)

Putting arrays in ArrayList

This question already has an answer here: Create ArrayList from array 32 answers You need to use Arrays.asList() function. String[] strings = {"ex","okay",}; ArrayList newStringslist = new ArrayList<Element>(Arrays.asList(strings)) Tutorials-Point Link Also - Create ArrayList from array new ArrayList<Element>(Arrays.asList(array)) -- JAVA per answer at Create ArrayList fr

将数组放入ArrayList中

这个问题在这里已经有了答案: 从数组32中创建ArrayList答案 您需要使用Arrays.asList()函数。 String[] strings = {"ex","okay",}; ArrayList newStringslist = new ArrayList<Element>(Arrays.asList(strings)) 教程 - 点链接 另外 - 从数组创建ArrayList new ArrayList<Element>(Arrays.asList(array)) - JAVA 从数组创建ArrayList的回答

Changing an Array to an ArrayList

This question already has an answer here: Create ArrayList from array 32 answers You need to create it like this: private ArrayList<String> booksList = new ArrayList<String>(Arrays.asList(books)); new ArrayList<String>(Arrays.asList(books)) is the part which is turning your array into an ArrayList . You could also do: private List<String> booksList = Arrays.asList

将数组更改为ArrayList

这个问题在这里已经有了答案: 从数组32中创建ArrayList答案 你需要像这样创建它: private ArrayList<String> booksList = new ArrayList<String>(Arrays.asList(books)); new ArrayList<String>(Arrays.asList(books))是将您的数组转换为ArrayList 。 你也可以这样做: private List<String> booksList = Arrays.asList(books); 如果它是一个ArrayList事实并不重要。 在这里回答 new Arra