Java Basics explained-----arrays

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.