Question

In: Computer Science

Describe how Java creates a single dimension array and a double dimension array. Describe and explain...

Describe how Java creates a single dimension array and a double dimension array.


Describe and explain how Java processes the contents of a single dimension array.


Describe and explain how Java saves values to a single dimension array, and to a double dimension array.

Solutions

Expert Solution

Array: Array is a collection of similar data types. In array all elements are the same data type. In array all elements are used the same data variable name but each element has own unique index number like a[0]. Array can use in all programming languages.

Type of array: two type of array

  • Single dimensional array(1D)
  • Multiple dimensional arrays (2D, 3D...)

Single Dimensional Array: one-dimensional array is a list of similar data types that are accessed by a single variable name. one-dimensional array is like a list. All elements are in a line.

Example of one-dimensional array and how it will declaration initialize and represent.

Declaration of the 1D array: Declaration and initialize of allocating memory to an array

Syntex: Datatype array_name = new datatype[size]

value syntex : array_ name [ ]= value ;

// initialize the first elements of the array

      arr[0] = 1 // initialize the second elements of the array

arr[1] = 2      //so on...

      arr[2] = 30;

      arr[3] = 40;

      arr[4] = 50;

example: int arr[] = new int[3]

{ where int = data type and arr[] = array name , 3 is number of space allocation in memory}

Use in program :

  1. //Java Program to illustrate how to declare, instantiate, initialize  
  2. class Test_array {  
  3. public static void main(String args[] ) {  
  4. int arr[]=new int[5]; //declaration and instantiation  
  5. arr[0]=10; //initialization  
  6. arr[1]=20;  
  7. arr[2]=70;  
  8. arr[3]=40;  
  9. arr[4]=50;  
  10. //traversing array  
  11. for(int i=0;i<arr.length;i++) //length is the property of array  
  12. System.out.println(arr[i]);  
  13. }}  

Multidimensional Array: If we want to save similar type of data in row and colume like table then we use 2D array. The most basic multidimensional array is 2D array.

let's see the example how we declare , initialize, and print 2d array.

Declaration of the 2D array: Declaration and initialize of allocating memory to an array

Syntex: Datatype array_name[ ][ ]= new datatype[ ][ ] ;

Initaialize: array_name[row_index][column_index] = value;

For example: arr_name[0][0] = 1;

Example: int arr_name[ ][ ] = new int[3][3]; // we allocate 3 row and 3 column in memory

Use of Two-Dimension Array in Program:

class class_name { // class intianlize

    public static void main(String[] args)

    {

   int[][] arr = { { 1, 2 }, { 3, 4 } }; // value initianlize

    for (int i = 0; i < 2; i++) // for loop for row

{

    for (int j = 0; j < 2; j++) // for loop for column

{

        System.out.print(arr[i][j] + " ");

}

   System.out.println();

   }

    }

}

  


Related Solutions

Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a...
Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a length of 15 that contains the first 15 numbers in the Fibonacci sequence and returns it. Set the first element to 0 and the second element to 1, then use a for loop to fill out the rest of the array.
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (explain briefly in java)
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {   ...
Java programming Create a application with a method void (after main() ) that creates an array...
Java programming Create a application with a method void (after main() ) that creates an array and asks for the user fill it with float numbers. This array have infinite elements until the user decide that it is enough and input a command to stop. Display this array's elements data in another method void. Thanks for your help!
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text and String key consisting of integers only, such that int rows=(int)Math.ceil(text.length()/key.length()); // or int rows=(int)Math.ceil(text.length()/key.length()); ? int columns= key.length(); int remainder= text.length() % key.length(); // such that the last row avoids taking an index beyond the string text by making columns - remainder The method fills the 2d array with letters a String entered by the use (row by row)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT