Java Learning Note 5 (Introduction to Classes and ArrayList)

Source: Internet
Author: User

1. The concept of class: to abstract real-life things into code (classes), we can use a custom array type (class) to describe things in real life.

2. Analysis: Using a mobile phone to analyze, mobile phone can call, surf the Internet, listen to music, these are methods, mobile phones have model, color, size, these are attributes.

Example:

Write a mobile phone class,

 Public class phone{    String color;    String brand;     Double

Write a test class, and the phone class in a folder,

 Public class testphone{    publicstaticvoid  main (string[] args) {        new  Phone ();         = "Whilte";         = "Vivo";         = 5.0;        System.out.println (P.color+ "        " +p.brand+ ""        +p.size+ "inch");}    }

Operation Result:

Memory Analysis:

1.testphone.class into the method area, there is only one main method

2.phone.class into the method area with three properties

3.main method runs into the stack

4. At new phone, the JVM opens up memory space in the heap to store the phone object and get the address

5. Three properties in the class, followed by the container into the heap memory, and given a default value (null,null,0.0)

6.P point to this memory address

7.p.color is a color attribute found in memory space, and is assigned a value

ArrayList Brief Introduction:

collection, unlimited in length. A reference type is stored, and the base type is not stored, such as int,double cannot be written, and it is changed to the appropriate reference type.

Create (phone is a class previously written):

Import java.util.ArrayList;  Public class arraylistdemo{    publicstaticvoid  main (string[] args) {        ArrayList  New arraylist<string>();        ArrayListNew arraylist<integer>();        ArrayListNew arraylist<phone>();}    }

Method:

Importjava.util.ArrayList; Public classarraylistdemo{ Public Static voidMain (string[] args) {ArrayList<String> array =NewArraylist<string>(); Array.add ("C + +");//adding elementsArray.add ("Python"); Array.add ("Java"); Array.add ("PHP"); intSize = Array.size ();//the size of the collectionSYSTEM.OUT.PRINTLN (size);//4String s = array.get (1);//get ElementSystem.out.println (s);//python//Traverse         for(inti = 0; I < array.size (); i++) {System.out.println (Array.get (i)); }                //Other methodsArraylist<integer> array2 =NewArraylist<integer>(); Array2.add (1); Array2.add (2); Array2.add (3); Array2.add (4); Array2.add (2,7);//Add 7 to the 2 indexArray2.set (0,10);//0 Index set to tenArray2.remove (4);//Delete         for(intj = 0; J < Array2.size (); J + +) {System.out.println (Array2.get (j)); }        //10,2,7,3array2.clear (); //clears the elements in the collection, and the container is still    }}

Java Learning Note 5 (Introduction to Classes and ArrayList)

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.