Many times there is a need to store a lot of data at one time. Collecting, storing, organizing, and calculating with many data points at once is possible using JAVA structured data.
Arrays
The most common form of storing a collection of data, arrays can store a large quantity of data having the same type while giving access to each element based on its position in the array structure.
The position of each element in an array is numbered starting with zero like the following :
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ... ]
First, an array must be declared using the following format:
type [ ] name = new type[length];
You can access and assign value to an array element using its index position number with the following syntax:
name[index] = value;
For example:
int [ ] new_Array = new int[3];
new_Array[0] = 10;
new_Array[1] = 100;
new_Array[2] = 1000;
This generates an array that looks like the following:
[ 10 100 1000 ]
You can also use a loop to quickly access all elements of an array:
for (int i=0; i < 3; i++) {
out.println( new_Array [i] );
}
You can access the length of the array at any time using the format:
name.length
This can assist in loop test condition definition:
for (int i=0; i < new_Array.length; i++) {
out.println( new_Array [i] );
}
Quick Array Initialization
It is possible to initialize and define an array in a single step like the following:
int [] integer_Array = { 5, -10, 45, 16, 3, 0, 25};
User Input Array
It is common to take data provided as user input from the app interface and store it as an array.In this example the process class is used to get a user string type input called enter. The data in the string is separated into an array using the split method at each blank in the string. The array called string_Array is used to store the separated string type data components.
public void process (String enter) {
String [] string_Array = enter.split(" ");
}
int [] integer_Array = new int[string_Array.length];
Next, the string components need to be stored into the new array as integers, in this example the total of each component is also computed.
int sum_Array = 0;
for (int i=0; i <integer_Array.length; i++) {
integer_Array[i] = Integer.parseInt(string_Array[i]);
sum_Array + =integer_Array[i];
}
Array Methods
Just as before with singular objects/variables, methods can be created and act on whole arrays. Remember that method creation is done to assign a group of code statements a unique name so that set of operations can be reused over and over again without having to re-type for each use.
Methods have parameters passed by the caller such as integers passed to the Math.max method. Just as in previous method creation, first the method name and the parameter types and names are initialized:
Methods have parameters passed by the caller such as integers passed to the Math.max method. Just as in previous method creation, first the method name and the parameter types and names are initialized:
public static type methodName (type [] paramName) {
Calling the method then looks like:
methodName(arrayName);
This call passes the array values in arrayName to the parameter paramName, the paramName array only exists inside the method {}
For example, if you wanted to created an array method for calculating the average of values in an array, the method and it's call would look like:
//Method to Average values in an array
public static double averageArray (double [] param) {
int sumArray = 0;
for (int i=0; i<param.length; i++) {
sumArray=sumArray+param[i];
}
return(double) sumArray/param.length;
}
//Calling the averageArray method
public void process () {
double [ ] classGrades = {98.5, 85.1, 75.4, 92.4, 94.6, 81.2};
double classAverage = averageArray(classGrades);
out.println ("The class average is " + classAverage);
}
You can also return an entire array by modifying the return type in the method initialization:
public static type [ ] methodName (parameters) {
For example, if you want to write a method that returns an array that repeats its original data you would write this:
//Method to repeat an array
public static int [ ] doubleArray ( int [] origArray ) {
int [ ] doubled = new int [2*origArray.length];
for (int i=0; i<origArray.length; i++) {
double[i] = origArray[i];
double[double.length + i -1] = origArray[i];
}
return doubled;
}
//Calling the doubleArray method
public void process () {
int [] dataArray = {17, 25, 19, 3, 95};
int [] dataDouble = doubleArray(dataArray);
}
Returns an array to dataDouble like:
{17, 25, 19, 3, 95, 17, 25, 19, 3, 95};
Arrays Class
The Array Class also has several useful built in methods that can act on arrays. This is similar to the Math Class or String Class with their built-in methods to act on numbers and strings.
Common Arrays Methods
Converts an array into a string
toString(arrayName)
Checks to see if two arrays are equal - returns "true" or "false"
equals(array1, array2)
Sets every component of an array to a set value
fill(arrayName, value)
Puts the array components in ascending order
sort(arrayName)
Returns the index location of a specific value in an array, returns negative if value is not found - and array must already be sorted: sort(arrayName)
binarySearch(arrayName, value)
For example:
//Import Array Class
import.java.util
//Using Arrays Class
int [] dataArray = {17, 25, 19, 3, 95};
Arrays.sort(dataArray);
The array dataArray now looks like: [3, 17, 19, 25, 95]
Arrays of Object Types
If you want to use an object type in an array instead of our common primitive types you need a two-step initialization process.
First, initialize the array with the object type you want to use:
Point [ ] coordArray = new Point[3];
Then you have to create a new object for each array component:
for( int i=0; i < coordArray.length; i++) {
coordArray[i] = new Point(0,0);
}
This creates an array with 3 components with Point object types like this:
[(0,0) , (0,0), (0,0)]
Multi-Dimensional Arrays
If you want to create an array with additional dimensions (like the array J in the picture at the top of this section having a 3 x 5 or "3 by 5" array) you will initialize the array using additional [] representing each new dimension you want to add
For example:
int [] [] J_array = new int [3] [5];
No comments:
Post a Comment