Java 如何使用 1-1000 的整数创建 100 的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22826953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To create Array Of 100 With Integers From 1-1000?
提问by user3478869
I know how to create an array of 100 with integers from 1-100 which will be just:
我知道如何用 1-100 的整数创建一个 100 的数组,这将是:
int [] array = new int[100]; // Sorted Array of 100
for (int a = 0; a < array.length; a++) {
array[a] = a + 1;
}
But my question is how to create an array of 100 with some sorted integers from 1-1000, inclusive. Any help will be appreciated!
但我的问题是如何创建一个 100 的数组,其中包含 1-1000 之间的一些排序整数。任何帮助将不胜感激!
采纳答案by aa8y
How about this?
这个怎么样?
int [] array = new int[100];
for (int a = 0; a < array.length; a++) {
array[a] = (a + 1) * 10;
}
Simple, if you have no other requirement.
很简单,如果你没有其他要求。
Edit: To make it almost sorted (like every 10th unsorted element), there are many ways. One, using BevynQ's solution, can be:
编辑:为了使它几乎排序(就像每 10 个未排序的元素一样),有很多方法。一,使用 BevynQ 的解决方案,可以是:
Random r = new Random();
int [] array = new int[100];
for (int a = 0; a < array.length; a++) {
if ((a + 1) % 10 != 0) {
array[a] = (a + 1) * 10;
} else {
array[a] = r.nextInt(1000);
}
}
回答by BevynQ
Here is a simple solution using random
这是一个使用随机的简单解决方案
Random r = new Random();
int [] array = new int[100];
int last = 0;
for (int a = 0; a < array.length; a++) {
last = last + r.nextInt(10) + 1;
array[a] = last;
}
回答by The Great
You can even create an array with data elements of a particular sequence such as prime numbers, factors or some series like fibonacci series.
您甚至可以创建一个包含特定序列的数据元素的数组,例如素数、因子或某些序列(如斐波那契数列)。
Example:
例子:
class Fibonacci {
public static void main(String args[]) {
int array[] = new int[100];
System.out.println("*****Fibonacci Series*****");
int f1, f2=0, f3=1;
for(int i=1;i<=100;i++) {
array[i] = f3;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
}
回答by Akash Patel
You can even make it so how sorted it is can easily be changed by the user. This is a lot more code to write, but works in essence by swaping a certain number of spots in the array. That number can change by the user. I put 0 to 100 before swaping the numbers, but all that matters is it is a well orderd math pattern.
你甚至可以让它的排序方式可以很容易地被用户改变。这是要编写的更多代码,但本质上是通过交换数组中的一定数量的点来工作的。该数字可以由用户更改。我在交换数字之前将 0 设置为 100,但重要的是它是一个有序的数学模式。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package partlysorted;
import java.util.Scanner;
/**
*
* @author Library computer
*/
public class PartlySorted {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//scanner for user input
Scanner input = new Scanner(System.in);
//intro
System.out.println("Welcome to the partly sorted pogram");
System.out.println("This will make a partly sorted list of integers");
//the numbers
int[] nums = new int[100];
//how unsorted for it to be
int suffels = -1;
//when to show a typo message
boolean firstLoop = true;
while(suffels < 0 || suffels > 100)
{
if(firstLoop)
{
System.out.println("Please enter how sorted sorted you want (0 to 100, no decimals)");
}
else
{
System.out.println("Looks like you made a typo");
System.out.println("Please enter a integer from 0 to 100");
}
suffels = input.nextInt();
firstLoop = false;
}
//fill it sorted first
for(int i = 0; i < nums.length; i++)
{
nums[i] = i;
}
//suffle the array
for(int swaps = 0; swaps < suffels; swaps++)
{
int firstPlace = (int)(Math.random() * 100);
int secondPlace = (int)(Math.random() * 100);
//swap the places
int temp = nums[firstPlace];
nums[firstPlace] = nums[secondPlace];
nums[secondPlace] = temp;
}
//printing it out
for(int n: nums)
{
System.out.println(n);
}
}
}