Create and array. Example Array in class: //Since we\'ll use sort method in the
ID: 3628990 • Letter: C
Question
Create and array.Example Array in class:
//Since we'll use sort method in the Arrays class which is in util package,
//, we need to import it:
import java.util.Arrays;
public class ArraysInClass {
//constructor:
public ArraysInClass(){
}
public static void main(String args[]) {
//The following shows how to refer to elements in an array:
int [ ] unitsSold = {10, 5, 2, 7, 8};
int total = 0;
for (int index = 0; index< unitsSold.length; index++)
{
total += unitsSold[index];
//use the index to refer to an element in the array
}
System.out.println("Now, I know how to refer to elements in the array");
System.out.println("The total is: " + total);
//The following shows how to sort an array:
String [] names = {"Mark", "James", "Jeff", "Jason", "Basil"};
System.out.print("The names before sorting are: ");
//Write the statements to display elements in the array
//before sorting using counter-controlled for statement:
//Now, this time, use enhanced for statement to display elements
//in the array:
System.out.println("Happy Sorting...");
//To sort, pass the names array to the sort method of class Arrays:
Arrays.sort(names);
System.out.print("The names after sorting are: ");
//Write the statements to display elements in the array after sorting:
}
}
Explanation / Answer
please rate - thanks
//Since we'll use sort method in the Arrays class which is in util package,
//, we need to import it:
import java.util.Arrays;
public class ArraysInClass {
//constructor:
public ArraysInClass(){
}
public static void main(String args[]) {
//The following shows how to refer to elements in an array:
int [ ] unitsSold = {10, 5, 2, 7, 8};
int total = 0;
for (int index = 0; index< unitsSold.length; index++)
{
total += unitsSold[index];
//use the index to refer to an element in the array
}
System.out.println("Now, I know how to refer to elements in the array");
System.out.println("The total is: " + total);
//The following shows how to sort an array:
String [] names = {"Mark", "James", "Jeff", "Jason", "Basil"};
System.out.print("The names before sorting are: ");
//Write the statements to display elements in the array
//before sorting using counter-controlled for statement:
for (int index = 0; index< names.length; index++)
System.out.print(names[index]+" ");
System.out.println();
//Now, this time, use enhanced for statement to display elements
//in the array:
for(String firstName : names)
System.out.print(firstName+" ");
System.out.println();
System.out.println("Happy Sorting...");
//To sort, pass the names array to the sort method of class Arrays:
Arrays.sort(names);
System.out.print("The names after sorting are: ");
//Write the statements to display elements in the array after sorting:
for(String firstName : names)
System.out.print(firstName+" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.