Java 11_ArrayList

作者: 綿綿_ | 来源:发表于2018-09-03 17:46 被阅读0次

ArrayList

import arraylist libraries

import java.util.ArrayList; // The ArrayList library
import java.util.Iterator; // The Iterator Library
import java.util.Arrays; // The Arrays Library

Create an ArrayList variable

        // You don't have to declare the ArrayList size like you
        // do with arrays (Default Size of 10)
    
        // You can create the ArrayList on one line
        
        ArrayList arrayListTwo = new ArrayList();
        
        // You can also define the type of elements the ArrayList
        // will hold
        ArrayList<String> names = new ArrayList<String>();
Add elements to an ArrayList
        names.add("John Smith");
        names.add("Mohamed Alami");
        names.add("Oliver Miller");
        
        // add an element in a specific position
        names.add(2, "Jack Ryan");
        
Retrieve values in an ArrayList with get
        // arrayListName.size() returns the size of the ArrayList
        for( int i = 0; i < names.size(); i++)
        {
            System.out.println(names.get(i));
        }
Replace a value using the set method
        names.set(0, "John Adams");
Remove an item with remove
        names.remove(3);

You can also remove the first and second item with the removeRange method

 names.removeRange(0, 1);
Print out the ArrayList itself use the toString method
        System.out.println(names);

You can also use the enhanced for with an ArrayList

        for(String i : names)
        {
            System.out.println(i);
        }
        System.out.println(); 
Use an iterator to print out values in an ArrayList
        // Creates an iterator object with methods that allow
        // you to iterate through the values in the ArrayList
        Iterator indivItems = names.iterator();
        
        // When hasNext is called it returns true or false
        // depending on whether there are more items in the list
        
        while(indivItems.hasNext())
        {
            // next retrieves the next item in the ArrayList
            System.out.println(indivItems.next());
            
        }
Copy in Arrays
    // I create an ArrayList without stating the type of values
    // it contains (Default is Object)
    ArrayList nameOne = new ArrayList();

    // addAll adds everything in name to nameOne
    nameOne.addAll(names);
    System.out.println(nameCopy);
Contains returns a boolean value based off of whether the ArrayList contains the specified object
        if(names.contains(paulYoung))
        {
            System.out.println("Paul is here");
        }
        
        // containsAll checks if everything in one ArrayList is in
        // another ArrayList
        if(names.containsAll(nameOne))
        {
            System.out.println("Everything in nameOne is in names");
        }
Deletes everything in the ArrayList
        names.clear();
        
        // isEmpty returns a boolean value based on if the ArrayList 
        
               // is empty(check)
        if (names.isEmpty())
        {
            
            System.out.println("The ArrayList is empty");
            
        }
ToArray & ToString
    Object[] moreNames = new Object[4];

    // toArray converts the ArrayList into an array of objects
    moreNames = nameOne.toArray();
    
    // toString converts items in the array into a String
    System.out.println(Arrays.toString(moreNames));

相关文章

  • Java 11_ArrayList

    ArrayList import arraylist libraries Create an ArrayList ...

  • Java(JavaEE)学习线路图1

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • Java学习线路图

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 大数据学习线路图

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 大数据学习教程

    Java教程 Java 教程Java 简介Java 开发环境配置Java 基础语法Java 对象和类Java 基本...

  • 一篇文章,全面解读Android面试知识点

    Java Java基础 Java集合框架 Java集合——ArrayList Java集合——LinkedList...

  • java学习路线

    javaSE java基础语法 java文件操作 java网络操作 java多线程 java数据库操作 java ...

  • java编程分类

    一、java简介java开发环境配置java基础语法java对象与类java基本数据类型java变量类型java修...

  • Java-01初识Java

    Java的3大版本 Java有三大版本,Java SE, Java ME, Java EE Java SE(Jav...

  • JAVA循环语句(四)9.29

    Java条件 if Java条件if...else Java条件多重if Java条件语句 嵌套if Java条件...

网友评论

    本文标题:Java 11_ArrayList

    本文链接:https://www.haomeiwen.com/subject/klqiwftx.html