two array structures in java

h‮//:sptt‬www.lautturi.com
two array structures in java

In Java, there are two main types of array structures:

  1. Single-dimensional arrays: These are arrays with a single dimension, also known as "vectors". Single-dimensional arrays are used to store a sequence of elements of the same type. Here is an example of how to create and initialize a single-dimensional array in Java:
// Create an array of integers with 5 elements
int[] myArray = new int[5];

// Initialize the array with values
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;

You can also use the shorthand syntax to create and initialize a single-dimensional array in Java:

int[] myArray = {1, 2, 3, 4, 5};
  1. Multidimensional arrays: These are arrays with multiple dimensions, also known as "matrices". Multidimensional arrays are used to store multiple sequences of elements of the same type in a single data structure. Here is an example of how to create and initialize a 2D array (matrix) in Java:
// Create a 2D array with 3 rows and 4 columns
int[][] myMatrix = new int[3][4];

// Initialize the array with values
myMatrix[0][0] = 1;
myMatrix[0][1] = 2;
myMatrix[0][2] = 3;
myMatrix[0][3] = 4;
myMatrix[1][0] = 5;
myMatrix[1][1] = 6;
myMatrix[1][2] = 7;
myMatrix[1][3] = 8;
myMatrix[2][0] = 9;
myMatrix[2][1] = 10;
myMatrix[2][2] = 11;
myMatrix[2][3] = 12;

You can also use the shorthand syntax to create and initialize a multidimensional array in Java:

int[][] myMatrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
Created Time:2017-10-17 22:51:24  Author:lautturi