Data Structures
Data Structures
Briefl
y explain how you can declare an array, initialize and access the
values in that particular array. An example will be very helpful.
Declare Here's how you can declare an array in
Java:Datatype[] arrayname;Double[] data;Here data is an
array that can hold values of type doubleInitializeHere's how
you can initialize an array during declaration.Int[ ] age = {12,
4, 5, 2, 5};This statement creates an array and initializes it
during declaration.The length of the array is determined by
the number of values provided which is separated by
commas. In our example, the length of age array is
5.AccessYou can easily access and alter array elements by
using its numeric index. .class ArrayExample { public static
void main(String[] args) {int[] age = new int[5]; // insert 14 to
third element age[2] = 14; // insert 34 to first element..age [0]
= 34;for (int i = 0; i < 5; ++i) { System.out.println("Element at
index " + i +": " + age[i]); } }}When you run the program,
the output will be:Element at index 0: 34Element at index
1: 0Element at index 2: 14Element at index 3: 0Element at
index 4: 0Sort the following list of elements using insertion
sort.23 78 45 8 32…23 45 78 8 32…8 23 45 78 32...8 23 32
45 78...8 23 32 45 78