Java Tour (18)--Basic data type Object wrapper class, collection frame, data structure, collection,arraylist, iterator iterator,list use

Source: Internet
Author: User

Java Tour (18)--Basic data type Object wrapper class, collection frame, data structure, collection,arraylist, iterator iterator,list use

Java defines all things as objects, and we want to use data types that can be referenced.

I. Object wrapper classes for basic data types

The left is the base data type, and the reference data type

    • BYTE byte
    • int Integer
    • Long Long
    • Boolean Booleab
    • float float
    • Double Double
    • Char Character

Let's take an integer to give an example.

//整数类型的最大值/最小值sop("最大值:"+Integer.MAX_VALUE);sop("最小值:"+Integer.MIN_VALUE);

Output

Basic data type Object wrapper class the most common effect

    • is the conversion between the basic data type and the string data type

      • The base data type is turned into a string

        Basic data type + ""
        Integer.tostring (34)

      • The string is converted to the base data type

        // 将一个字符串转为整数int num = Integer.parseInt("123");sop("值:"5));

        Results of the output

Like other use methods are similar, but there is a special, that is a Boolean, a true one is "true", again, such as you pass the ABC to int type, this is wonderful, he will report the data format is abnormal

Of course, there are all kinds of conversion, white is a few methods, we can study, here do not do more than repeat

We actually come up with a small example to highlight their characteristics.

 PackageCom.lgl.hellojava;//public class class name Public  class Hellojjava {     Public Static void Main(string[] args) {/** * Object wrapper class for basic data type * /Integer x =NewInteger ("123"); Integer y =NewInteger (123);//AskSop"x = = y:"+ (x = = y)); Sop"X.equals (y):"+ (X.equals (y))); }/** * Output * *     Public Static void SOP(Object obj)    {System.out.println (obj); }}

What is the result of this?

This is a good understanding.

New features after the JDK1.5 version

    • Automatic Boxing
Integer=newInteger(4);Integer=4;  //自动装箱=+2//进行了自动拆箱,变成了int类型,和2进行加法运算,再将和进行装箱,赋给x

One more interesting example.

        Integer128;        Integer128;        Integer127;        Integer127;        sop("x == y :" + (x == y));        sop("i == j : " + (i == j));

How much output is there?

Why is that?

    • Because I and J are the same integer object, within the byte range, if the value already exists for the new attribute, no new space will be opened
Two. Collection Framework

After we finish talking about the data type of the assorted, we'll start with the data type store, first we talk about the collection

    • Why collection classes appear

      • Object-oriented language is the embodiment of things in the form of objects, so in order to facilitate the operation of multiple objects, the object is stored, set is the most common way to store objects
    • How do arrays and collection classes differ from one container to another?

      • Arrays can also store objects, but the length is fixed, the set length is variable, the data type can be stored in the array, the collection can only store objects
    • Features of the collection

      • Collections are used only to store objects, the collection length is mutable, and collections can store different types of objects

The collection frame is continuously drawn upward.

Why are there so many containers?

    • Because each container has a different way of storing data, this storage method we call it: Data structure

We will learn this data structure in turn

Three. Collection

Root interface, let's learn their common approach

 PackageCom.lgl.hellojava;ImportJava.util.ArrayList;//public class class name Public  class Hellojjava {     Public Static void Main(string[] args) {/** * Collection * *        //Create a collection container that uses subclasses of the collection interface ArrayListArrayList list =NewArrayList ();//Add elementList.add ("Hello 0"); List.add ("Hello 1"); List.add ("Hello 2"); List.add ("Hello 3"); List.add ("Hello 4");//Gets the length of the collectionSop"Length:"+ list.size ()); }/** * Output * *     Public Static void SOP(Object obj)    {System.out.println (obj); }}

It's written like this, and here's the question, why is the Add parameter an object?

    • The parameter type of the 1.add method is object, which makes it easy to receive objects of any type
    • 2. The collection is stored in the reference and address of the object,

So we can also

 //gets the length of the collection  SOP ( "Length:"  + list . Size ());        //Print collection  sop (list );        //Delete element         list . Remove ( "Hello 1" );        SOP (list ); //judgment  SOP ( "Hello 3 exists:"  +list . Contains (" Hello 3 "));        //empty         list . Clear ();        //is null  SOP (list . IsEmpty ()); SOP (list );  

Come to the conclusion

Let's talk about one more intersection.

 Public Static voidMethod_2 () {ArrayListList=NewArrayList ();//Add element        List. Add ("Hello 0");List. Add ("Hello 1");List. Add ("Hello 2");List. Add ("Hello 3");List. Add ("Hello 4"); ArrayList List1 =NewArrayList ();//Add elementList1.add ("Java 0"); List1.add ("Hello 1"); List1.add ("Java 2"); List1.add ("Hello 3"); List1.add ("Java 4");//Fetch intersection list will only retain the same elements as the List1        List. Retainall (List1); SopList); }

List will only retain the same elements as the List1

Four. Iterator iterator

Let's go back to the iterator, which is how to take out the data operation

/**     * 迭代器     */    publicstaticvoidmethod_get() {        new ArrayList();        // 添加元素        list.add("Hello 0");        list.add("Hello 1");        list.add("Hello 2");        list.add("Hello 3");        list.add("Hello 4");        //获取迭代器,用于取出集合中的元素        Iterator iterator = list.iterator();        while (iterator.hasNext()) {            sop(iterator.next());        }    }

So we can print it all out.

So what do we understand about iterators?


    • is actually the way the collection takes out the elements.

The extraction method is defined in the collection of the internal, so that the way to access the collection of the contents of the collection, then the extraction method will be defined as an internal class, and each container data structure is different, so take out the action details are different, but there are common content, judgment, take out, then you can write generic extraction, So these inner classes conform to a rule, which is iterator, how do you get the collection's Fetch object? Through an externally available method Interator ();
The general direction we have mastered, so that we should be broken down to talk about, we first say list

Six. List

Collection there are two sub-interfaces, list and set, the difference between the two is

    • List: elements are ordered and elements can be duplicated because the collection system has an index
    • Set: elements are unordered, elements cannot be duplicated, cannot be indexed

All we have to say is list, the generality of it is not said, we, he alone

List-specific methods: Any method that can manipulate the table is a unique method of the system, that is,

    • Increase
      • Add (index,element)
      • AddAll (index Collection);
    • By deleting
      • Remove (Index)
    • Change
      • Set (Index)
    • Check
      • Get (Index)
      • Sublist (From,to)
      • Listiterator ()

Let's say it all over again, this is the old routine, we even summed up the previous knowledge

 PackageCom.lgl.hellojava;ImportJava.util.ArrayList;ImportJava.util.Iterator;//public class class name Public  class Hellojjava {     Public Static void Main(string[] args) {/** * List * *ArrayList Al =NewArrayList ();//Add elementAl.add ("Java 0"); Al.add ("Java 1"); Al.add ("Java 2"); Sop"Original collection:"+ AL);//Add element at specified locationAl.add (1,"Java 3"); Sop"After adding:"+ AL);//Delete the element at the specified positionAl.remove (2); Sop"After deletion:"+ AL);//Modify elementAl.set (2,"Java 4"); Sop"After modification:"+ AL);//Get elements through a corner markSop"Find:"+ Al.get (1)); Sop"All elements");//Get all elements         for(inti =0; I < al.size (); i++) {SOP ("Element"+ i +" = "+ Al.get (i)); } SOP ("Iterator");//via iteratorsIterator it = Al.iterator (); while(It.hasnext ()) {SOP ("Next:"+ It.next ()); }    }/** * Output * *     Public Static void SOP(Object obj)    {System.out.println (obj); }}

This covers a lot of the list of knowledge points, constantly upward extraction of a process, the results of our output

OK, that way, we will be here for this lesson, OK, thank you for watching so long, tired drink a glass of water it!

My group: 555974449, Welcome to your arrival!

Java Tour (18)--Basic data type Object wrapper class, collection frame, data structure, collection,arraylist, iterator iterator,list use

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.