Array Based on Java

Source: Internet
Author: User

 

Arrays are a special type of data that has been mentioned in many basic courses.

1. Create an array

Int [] num1 = new int [3];

Int [] num2 = new int [] {1, 2, 3 };

Int [] num3 = {1, 2, 3 };

Ask a small question. Is this definition correct? Int [] num3 = new int [3] {1, 2, 3}; Answer: No! This is what Zhang Xiaoxiang's video knows.

String [] str1 = new string [3];

String [] str2 = new string [] {"A", "B", "C "};

String [] str3 = new string {"A", "B", "C "};

2. array Initialization

Dynamic initialization: the array definition is separated from the operations for allocating space and assigning values to arrays;
Static initialization: when defining numbers, allocate space for array elements and assign values;
Default initialization: The array is a reference type, and its elements are equivalent to the member variables of the class. Therefore, after the array is allocated space, each element is also initialized by the hermit according to the rules of the member variables.

If you initialize the array while declaring it, do not specify the number of elements in the array. Otherwise, you must specify the number of arrays.

3. Print the Array
We know that printing an array only prints the first address of the array. What we really want to know is what data is stored in the array. Fortunately, we can print out the array in the following way,

Use arrays. aslist () as an example:

package test;import java.util.Arrays;public class ShuZu {  public static void main(String[] args) {    int []num ={1,2,3,4};  String []str = new String[]{"a","b","c"};    System.out.println(Arrays.asList(num));  System.out.println(Arrays.asList(str)); }}

 

Output result:

[[I @ de6ced]
[A, B, c]
The following output result is obtained because arrays. aslist () requires the object type parameter aslist (object [] OBJ) in versions earlier than jdk1.5)

Int is a common type and does not meet all requirements. Therefore, the system automatically calls the arrays. aslist (T... A) method of JDK, which will

As a parameter is passed in, only the address is printed above, and the character array is printed according to our requirements.

The solution is to define an integer array as an integer. Integer [] num = new INTEGER {1, 2, 3 };

 

 

 

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.