datatype[] arrayRefVar;
Example:
double[] myList;
datatype arrayRefVar[]; // This style is allowed, but not preferred
Example:
double myList[];
datatype[] arrayRefVar = new datatype[arraySize];
double[] myList = new double[10];
datatype arrayRefVar[] = new datatype[arraySize];
double myList[] = new double[10];
Once an array is created, its size is fixed. It cannot be changed. You can find its size using
public class TestVoidMethod {
double[] arrayRefVar = new double[10];
arrayRefVar.length
For example,
arrayRefVar.length returns 10
}
When an array is created, its elements are assigned the default value of
0 for the numeric primitive data types,
'\u0000' for char types, and
false for boolean types.
double[] myList = {1, 2, 3, 4};
myList[2] = myList[0] + myList[1];
System.out.println(myList[2]);
double[] myList = {1.9, 2.9, 3.4, 3.5};
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars[0]);
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]); // Now outputs Opel instead of Volvo
double[] myList = {1.9, 2.9, 3.4, 3.5};
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
//This is incorrect
double[] myList;
myList = {1.9, 2.9, 3.4, 3.5};
//This is correct
double[] myList= {1.9, 2.9, 3.4, 3.5};
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter " + myList.length + " values: ");
for (int i = 0; i < myList.length; i++) {
myList[i] = input.nextDouble();
}
for (int i = 0; i < myList.length; i++) {
myList[i] = Math.random() * 100;
}
double[] myList = {1, 2, 3, 4};
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i] + " ");
}
// Array with random number
int[] myList1 = new int[2];
for (int i = 0; i < myList1.length; i++) {
myList1[i] = (int) (Math.random() * 100);
System.out.println( myList1[i] );
}
//Sum array values
double[] myList = {1, 2, 3, 4};
int total=0;
for (int j = 0; j < myList.length; j++) {
total+=myList[j];
}
System.out.println( total);
double[] myList = {1, 2, 3, 4};
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows: list2 = list1;
# Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
for (int i = 0; i < sourceArrays.length; i++)
targetArray[i] = sourceArray[i];
public static void main(String[] student) {
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 5, 10, 20, 30, 40, 50 };
System.arraycopy(arr1, 0, arr2, 0, 2);
System.out.print("arr2 = ");
System.out.print(arr2[0] + " ");
System.out.print(arr2[1] + " ");
System.out.print(arr2[2] + " ");
System.out.print(arr2[3] + " ");
System.out.print(arr2[4] + " ");
}
/*
Parameters
src − This is the source array.
srcPos − This is the starting position in the source array.
dest − This is the destination array.
destPos − This is the starting position in the destination data.
length − This is the number of array elements to be copied.
Return Value
This method does not return any value.
*/
printArray(new int[]{3, 1, 2, 6, 4, 2});
//Linear Search code
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++) {
if (key == list[i])
return i;
}
return -1;
}
Github code sample Linear Search
Github code sample Binary Search