Thanks Using an ArrayList and an Array read in a sequence of

Thanks!

Using an ArrayList and an Array, read in a sequence of integers between 0 and 99 and print them in sorted order using a sort. Name one file ArrayListSort and the other ArraySort. For example, the sequence: 98 2 3 1 0 0 0 3 98 98 2 2 2 0 0 0 2 Should produce: 0 0 0 0 0 0 1 2 2 2 2 2 3 3 98 98 98

Solution

ArraySort.java

import java.util.Arrays;
import java.util.Scanner;

class ArraySort {

public static void main(String[] args) {

// initializing unsorted int array with input from user

Scanner sc = new Scanner(System.in);
System.out.println(\"Enter the numbers(between 0 and 99) seperated by spaces: \ \");
String input = sc.nextLine();
String[] split = input.split(\"\\\\s+\");
int[] iArr = new int[split.length];
int i=0;
for (String string : split) {
iArr[i++] = Integer.parseInt(string);
}   

// sorting array
Arrays.sort(iArr);

// let us print all the elements available in sorted list
System.out.println(\"The sorted int array is: \ \");
for (int number : iArr) {
System.out.print(number + \" \");
}
}
}

ArrayListSort.java

import java.util.*;
class ArrayListSort {

public static void main(String args[]){
ArrayList<Integer> arraylist = new ArrayList<Integer>();

// initializing unsorted int array with input from user

Scanner sc = new Scanner(System.in);
System.out.println(\"Enter the numbers(between 0 and 99) seperated by spaces: \ \");
String input = sc.nextLine();
String[] split = input.split(\"\\\\s+\");
for (String string : split) {
arraylist.add(Integer.parseInt(string));
}

/* Sorting of arraylist using Collections.sort*/
Collections.sort(arraylist);

/* ArrayList after sorting*/
System.out.println(\"The sorted int array is: \ \");
for(int number: arraylist){
System.out.print(number + \" \");
}
}
}

Thanks! Using an ArrayList and an Array, read in a sequence of integers between 0 and 99 and print them in sorted order using a sort. Name one file ArrayListSor
Thanks! Using an ArrayList and an Array, read in a sequence of integers between 0 and 99 and print them in sorted order using a sort. Name one file ArrayListSor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site