I know how to create an array of 100 with integers from 1-100 which will be just:
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!
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:
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);
}
}
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;
}
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;
}
}
}
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.
/*
* 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);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.