Java Basic Learning Path (v) definition of an array

Source: Internet
Author: User

What is an array: a heap of data of the same type (a set of related variables)

Definition syntax:

    1. Declare and open an array

Data type array name [] = new data type [length];

    1. Distribution complete
    • Declaring an array: Data type array name [] = null;
    • Open array: Array name = new data type [length];
 Public classTest2 { Public Static voidMain (String args[]) {intArr[] =New int[3]; arr[0] =1; arr[1] =2; arr[2] =3;  for(intI=0;i<3; i++) {System. out. println (Arr[i]); }    }}

Memory Analysis:

    1. Declare and open an array

2. Distribution Complete

Array Memory Reference

An array is a reference data type

 Public classTest2 { Public Static voidMain (String args[]) {intArr[] =New int[3]; arr[0] =1; arr[1] =2; arr[2] =3; intArr_tem[] =arr; arr_tem[0] = -;  for(intI=0;i<3; i++) {System. out. println (Arr[i]); }    }}//output to
- 2 3

All of the above methods are dynamic initialization of arrays, which are the first space to be set up for an array and then assigned. Here, you will be contacted with the previous method of constructing (and then creating the default value when instantiating the object), and the two static initialization methods of the array are described below.

Simple method: int arr[] = {value, value, value ...} Try not to use

Complete method: int arr[] = new int[] {value, value, value ...}

 Public class test2 {    publicstaticvoid  main (String args[]) {        int New int [] {A-I};         int arr_tem[] = arr;        arr_tem[0] = +;          for (int i=0;i<3;i++) {            System.out.println (arr[i]);     }}}

Java Basic Learning Path (v) definition of an array

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.