0 votes
I was making an array with 10 elements which consist of random values. Now i want to sort them in order of lowest to highest value. How would I write a code for the same?
Apr 17, 2018 in Java 627 views

## 3 answers to this question.

0 votes
```int[] arr = new int[10];
Random r = new Random();
for (int i = 0; i < arr.length; i++)
arr[i] = r.nextInt(100) + 1;
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
for (int i = arr.length - 1; i >= 0; i--)
System.out.print(arr[i] + " ");
System.out.println();```
answered Apr 17, 2018 by anonymous
0 votes
```public void bubbleSort(int[] array) {
boolean swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < array.length - j; i++) {
if (array[i] > array[i + 1]) {
tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
swapped = true;
}
}
}
}```
answered Jul 24, 2018 by
• 6,900 points
0 votes
```import java.util.Arrays;

public class Sort {
public static void main(String args[])
{
int [] array = new int[10];
for ( int i = 0 ; i < array.length ; i++ ) {
array[i] = ((int)(Math.random()*100+1));
}
Arrays.sort( array );
for ( int i = 0 ; i < array.length ; i++ ) {
System.out.println(array[i]);
}
}
}```
answered Aug 24, 2018 by
• 4,630 points

+1 vote
3 answers

## How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

0 votes
1 answer

## How to reverse an int array in Java?

for(int i = 0; i < Data.length ...READ MORE

0 votes
2 answers

## How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or  yourList.stream().forEach(a -> ...READ MORE

0 votes
2 answers

## How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

+1 vote
3 answers

## What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

0 votes
2 answers

## One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

0 votes
2 answers

## How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

0 votes
2 answers

## How to concatenate two arrays in Java?

public <T> T[] concatenate(T[] a, T[] b) ...READ MORE

0 votes
3 answers

## How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

0 votes
3 answers

## How to convert File to Byte[] in Java?

import java.io.File; import java.nio.file.Files; File file; // ...(file is initialised)... byte[] ...READ MORE