Learn java-chapter 1 variable (Variables)-Array (Arrays)

Source: Internet
Author: User
Tags array definition

The Java array determines the length of the array when it is created. Index subscript starting from 0.

1. Array definition and initialization

int[] anarray;//Definition Anarray = new int[2];//initialization anarray[0] = 100;//Assignment anarray[1] = 200;//Assignment System.out.println ("Element at IND Ex 0: "+ anarray[0]);//Use SYSTEM.OUT.PRINTLN (" Element at index 1: "+ anarray[1]);//Use

Program output:

Element at index 0:100element at index 1:200

2. Define initialization and assign values at the same time

Int[] Anarray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};

3. Multidimensional arrays

String[][] names = {"Mr.", "Mrs.", "Ms."}, {"Smith", "Jones"}; System.out.println (Names[0][0] + names[1][0]); System.out.println (Names[0][2] + names[1][1]);

Program output:

Mr smithms. Jones

As can be seen from the above example, the length of each row array in a Java multidimensional array does not require equality.

4. Copying an array

Class Arraycopydemo {public static void main (string[] args) {char[] CopyFrom = {' d ', ' e ', ' C ', ' A ', ' f ', ' F ',        ' E ', ' I ', ' n ', ' a ', ' t ', ' e ', ' d '};        char[] CopyTo = new CHAR[7];        System.arraycopy (CopyFrom, 2, CopyTo, 0, 7);    System.out.println (New String (CopyTo)); }}

Program output:

Caffein


Learn java-chapter 1 variable (Variables)-Array (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.