1. What is an array
2. How to use arrays
Package test;
public class Shuzu {
public static void Main (string[] args) {
/**
* 1. What is an array
* An array is a linear set of data with the same data type
* Use new to allocate memory space
*/
1. Initializing a one-dimensional array
int Arry[]=new int[]{1,2,3,4};
int arry2[]={1,3,5,6};
The subscript for the array starts at 0.
for (int i=0;i<arry.length;i++)
{
System.out.println (i);
}
2. Creating a two-dimensional array
int twoarry[][]={{1,2,3},{2,34,4}};
The method of traversing a two-dimensional array, first looping a few large arrays, in the loop of small arrays
for (int i=0;i<twoarry.length;i++)//with two large arrays
{
for (int j=0;j<twoarry[i].length;j++)//small with 3
{
System.out.print (twoarry[i][j]+ "");
}
System.out.println ();//Line break
}
int i=0;
for (int s[]:twoarry) {
i++;
int j=0;
for (int g:s)
{
j + +;
if (i==twoarry.length && j==s.length)
{
System.out.print (g);
}else{
System.out.println (g+ ",");
}
}
}//the second method ends
}
}
Java Basics explained-----arrays