繁体   English   中英

如何使用 1-1000 的整数创建 100 的数组?

[英]How To create Array Of 100 With Integers From 1-1000?

我知道如何用 1-100 的整数创建一个 100 的数组,这将是:

int [] array = new int[100];                       // Sorted Array of 100
for (int a = 0; a < array.length; a++) {
    array[a] = a + 1;
}

但我的问题是如何创建一个 100 的数组,其中包含 1-1000 之间的一些排序整数。 任何帮助将不胜感激!

这个怎么样?

int [] array = new int[100];
for (int a = 0; a < array.length; a++) {
    array[a] = (a + 1) * 10;
}

很简单,如果你没有其他要求。

编辑:为了使它几乎排序(就像每 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);
    }
}

这是一个使用随机的简单解决方案

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;
}

您甚至可以创建一个包含特定序列的数据元素的数组,例如素数、因子或某些序列(如斐波那契数列)。

例子:

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; 
        } 
    } 
}

你甚至可以让它的排序方式可以很容易地被用户改变。 这是要编写的更多代码,但本质上是通过交换数组中的一定数量的点来工作的。 该数字可以由用户更改。 我在交换数字之前将 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);
   }
  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM