Solved

Help on One - Dimensional Array

Posted on 2011-09-22
5
281 Views
Last Modified: 2012-06-21
//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]

0
Comment
Question by:bakerule22
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:a_b
ID: 36585271
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?
0
 

Author Comment

by:bakerule22
ID: 36585278
Not sure what write on part
//Write the statements to display elements in the array
                        //before sorting using counter-controlled for statement:
0
 
LVL 20

Expert Comment

by:a_b
ID: 36585286
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]);
            }
0
 
LVL 4

Accepted Solution

by:
reijnemans earned 500 total points
ID: 36585842
ane an enhanced for loop looks like this:

for (int value : unitsSold) {
   System.out.print(value);
}

Open in new window

0
 

Author Closing Comment

by:bakerule22
ID: 36936718
I did not get the answer that i wanted however thanks for the help
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now