Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: Java- Sorting Arrays
Description: These are notes defining and explaining how to sort Arrays in Java
Description: These are notes defining and explaining how to sort Arrays in Java
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
Question 1: Demonstrate how you can sort an array in ascending and descending order
An array can be sorted by importing the Arrays library and using the sort method
...
Ascending Order
package sorting;
import java
...
Arrays; //import Arrays library
import java
...
Scanner;
public class Sorting {
public static void main(String[] args) {
int marks[]=new int[4];
Scanner s=new Scanner(System
...
out
...
nextInt();}
Arrays
...
length;i++){
System
...
println("Mark:" + marks[i]); // show marks in ascending order
}
}}
Output: Mark: 24
Mark: 45
Mark: 87
Mark: 100
Descending Order
Arrays can be sorted in descending order using two methods:
1
...
Converting array to integer objects and then importing form the Collections library
package descending;
import java
...
Arrays;
import java
...
Collections;
import java
...
Scanner;
public class Descending {
public static void main(String[] args) {
int marks[]=new int[4];
Scanner s=new Scanner(System
...
out
...
nextInt();}
//10,25,78,80
Integer[] desc=new Integer[marks
...
sort(desc,Collections
...
out
...
array;
public class StudentArray {
public static void main(String[] args) {
int marks[][]={{75,100,84,76,100},{80,00,45,65,10},{10,45,34,78,97}};
for(int i=0;i<3;i++){for (int j=0;j<5;j++){System
...
println(marks[i][j]+"");}
}
System
...
println("");
}
}
Output: 75
100
84
76
100
80
0
45
65
10
10
45
34
78
97
2b) 4*5 array
package stringarray;
import java
...
Arrays;
public class Stringarray {
public static void main(String[] args) {
String
name[][]={{"Sekai","Sam","kim","kate","kitsi"},{"Kudzai","munhu","sekie","skye","vanhu"},{"
Donald","taku","gamu","ana","jose"},{"Sabelo","tarie","olie","tino","tadie"}};
for(int i=0;i<4;i++){for (int j=0;j<5;j++){System
...
println(name[i][j]+"");}}
System
...
println("");
} }
Question 3: For the array created in (2b), show how it can be sorted in ascending or
descending order per row
...
util
...
util
...
util
...
util
...
add(s);}
Collections
...
System
...
println(list);
} }}
Output: [kate, kim, kitsi, sam, sekai]
[kate, kim, kitsi, kudzai, munhu, sam, sekai, sekie, skye, vanhu]
[ana, donald, gamu, jose, kate, kim, kitsi, kudzai, munhu, sam, sekai, sekie, skye, taku, vanhu]
[ana, donald, gamu, jose, kate, kim, kitsi, kudzai, munhu, olie, sabelo, sam, sekai, sekie, skye,
tadie, taku, tarie, tino, vanhu]
Title: Java- Sorting Arrays
Description: These are notes defining and explaining how to sort Arrays in Java
Description: These are notes defining and explaining how to sort Arrays in Java