Link to home
Start Free TrialLog in
Avatar of bakerule22
bakerule22

asked on

Help on One - Dimensional Array

//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:
		



	}
}

Open in new window


Instruction
1.      Arrays are data structures that consist of data items of the same type (called elements).

2.      Each element is referenced by its index (position number starting from 0) within the array.

3.      Crate an array:
-      to declare an array, you provide the array’s type and name and use [ ].  For example, to have an array to save 5 students’ names:
String [ ] names;
-      to create the array after it has been declared:
                  names = new String[5];
-      to initialize the array:
                  names = {“Mark”, “James”, “Jeff”, “Jason”, “Basil”};
     
            - another way to create:
            Eg1:  String [ ] names = {“Mark”, “James”, “Jeff”, “Jason”, “Basil”};
                        Eg2:  int [ ] unitsSold = {10, 5, 2, 7, 8};

4.      Refer to individual elements of an array:  arrayName[index]

Avatar of a_b
a_b

Since this seems like a homework question we cannot provide a complete solution.
Post your attempt and we can help you out.

What is it that you need help in specifically?
Avatar of bakerule22

ASKER

Not sure what write on part
//Write the statements to display elements in the array
                        //before sorting using counter-controlled for statement:
The section here in the code snippet below shows you how to run a counter controlled statement.


for (int index = 0; index< unitsSold.length; index++)
            {
                  //You need to change is the section inside to display the array element by using     System.out.print(yourArray[index]);
            }
ASKER CERTIFIED SOLUTION
Avatar of reijnemans
reijnemans
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did not get the answer that i wanted however thanks for the help