Java Intermediate--Set frame "1"

Source: Internet
Author: User

Set Frame ----ArrayList

Introduction: Let's take a look at the use of traditional arrays

  1. write a hero object class

    package  cn.jse.t1;

    public  , class  hero {

    public  string Name;

    public   float  hp;

    public   int  damage;

     

    public  hero () {}

    //Add a constructor to initialize name

    public  hero (String name) {

    this . Name=name;

    }

    //Override a ToString method

    public  string toString () {

    return  name;

    }

    }

  2. Write a test class

    Package cn.jse.t1;

    Public class testcollection {

    Public Static void Main (string[] args) {

    Limitations of arrays

    Hero hero[] = new hero[10];

    Analysis: Declared an array of length 10, if not finished is a waste of memory, more than 10 and not fit

    Hero[0] = new Hero ("ADC");

    hero[11]= new Hero ("Mid"); There's more than 10 here and there will be an error.

    }

    }

  3. in order to solve this embarrassing situation, we can use the container class ArrayList, We add some code inside the test class.

    Package cn.jse.t1;

    import java.util.ArrayList;

    Public class testcollection {

    Public Static void Main (string[] args) {

    ...

    In order to solve the limitation of array, the concept of container class is introduced.

    ArrayList heros=new ArrayList();

    heros . Add (new Hero ("single"));

    Output the capacity of the current container

    System. out. println (Heros.size ());

    heros . Add (new Hero ("aux"));

    System. out. println (Heros.size ());

    The result is that the capacity of the container increases with the increase of the object, and the boundary problem of the array is not considered.

    }

    }

    We can look at the output of the console:

    1

    2

  4. we can take a look . Common methods of ArrayList:

    How to use: We also add new code (add,contains) to the test class

    Package cn.jse.t1;

    import java.util.ArrayList;

    Public class testcollection {

    Public Static void Main (string[] args) {

    ...

    Next we can look at the common methods of ArrayList

    arraylistNew arraylist

    Put five objects into a ArrayList

    for (int i=0;i<5;i++) {

    Heros2.add (new Hero ("Hero" + i));

    }

    System. out. println (heros);

    Add an object at the specified position such as in Subscript 3 position add a superhero

    Hero superhero = new Hero ("Super Hero");

    Heros2.add (3, superhero);

    Outputs all the hero in the array

    System. out. println (Heros2.tostring ());

    Determine whether an object is in a container by means of contains

    System.  out. println ("Then this Hero6 is not here?") The answer is: "+heros2.contains (new Hero (" Hero6 "));

    System.  out. println ("then this superhero is not here?") The answer is: "+heros2.contains (superhero));

    }

    }

    Console output:

    [Middle order, auxiliary]// This is because we have added 2 hero

    [Hero 0, Hero 1, Hero 2, Super hero, Hero 3, Hero 4]

    so this  Hero6 's not here? The answer is: false

    so this superhero is not here? The answer is: true

    Other methods are not listed, interested students can look at the relevant knowledge points

Java Intermediate--Set frame "1"

Related Article

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.