Written by: Timothy Sonniah

Describe what you achieve, other than additional complexity, when using arrays.

The use of arrays in programming leads to additional complexity and allows the programmer to have random access to all the elements in the collection. This property is quite essential in merge sorts sand other applications that require random access to the elements in the arrays.

 Explain whether you can store different data types and elements in the same array.

An array is defined as a collection of items. Therefore, arrays can contain different types of elements; nonetheless, elements of different data types cannot be stored in a single array. In a scenario where one would wish to store different data types in an array, one has to come up with a separate array for each of the data types present (Javatpoint).

Then explain where and when you would use a one dimensional and two-dimensional array. Be sure to provide code examples of declaration and initialization of one and two-dimensional arrays in a java program.

One Dimensional Array

One-dimensional arrays are used when the data to be stored only has a single piece of information, for example, in a linear array.

Syntax: int ar[]=new int[n];

//Java Program to illustrate how to declare, instantiate, initialize  

//and traverse the Java array.  

class Testarray{  

public static void main(String args[]){  

int a[]=new int[5];//declaration and instantiation  






//traversing array  

for(int i=0;i<a.length;i++)//length is the property of array  



Two-Dimensional Arrays

Two- dimensional arrays are used to store data types whereby the data carries more than a single piece of information.

 For instance:

Syntax: int ar[][]=new int[n][2];

class Test {

    public static void main(String[] args) {

        char[][] table = new char[3][3];

        for (int row = 0; row < 3; row ++)

            for (int col = 0; col < 3; col++)

                table[row][col] = (char) (‘1’ + row * 3 + col);

        for (int row = 0; row < 3; row ++)

            for (int col = 0; col < 3; col++)

                System.out.println (table[row][col]); (javatutoring)

Work Cited: Javatpoint. https://www.javatpoint.com/array-in-java. n.d.