• We just launched and are currently in beta. Join us as we build and grow the community.

ArrayLists

anahouwa

Anime Figurine Showcase Specialist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
46
Likes
97
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
Introduction:
Welcome! This tutorial will teach you about how to create and use ArrayLists.

What are ArrayLists?
ArrayLists, normally referred to as just Arrays, is a type which can contain multiple items. For example; a shopping list could be referred to as an array or shopping items.

ArrayLists must be made up of one type only. You are unable to mix and match different types within one ArrayList such as String and Integer. You are able to use custom created classes (using OOP) in ArrayLists.

How do I use ArrayLists?
ArrayLists are just like any other type of variable expect it is more common to use pre-made methods with ArrayLists compared to other variable types such as Integers. ArrayLists have a few essential functions which are used below such as; .add, .clear and .get.

Example:
Here is an example of creating a new ArrayList (of the String type), adding some items, outputting the total amount of items in list, outputting each item in the list, then clearing and re-outputting the total amount of items in the list.

  1. ArrayList<

    String>

    list =

    new

    ArrayList<

    String>

    (

    )

    ;
  2. list.add

    (

    "Hi"

    )

    ;
  3. list.add

    (

    "Hey"

    )

    ;
  4. list.add

    (

    "Hello"

    )

    ;
  5. System

    .out

    .println

    (

    "Total Items in List: "

    +

    list.size

    (

    )

    )

    ;
  6. for

    (

    int

    i=

    0

    ;

    i<

    list.size

    (

    )

    ;

    i++

    )

    {
  7. System

    .out

    .println

    (

    "Item in List "

    +

    i +

    " is "

    +

    list.get

    (

    i)

    )

    ;
  8. }
  9. list.clear

    (

    )

    ;
  10. System

    .out

    .println

    (

    "Total Items in List: "

    +

    list.size

    (

    )

    )

    ;

Which gives the output:

Total Items in List: 3
Item in List 0 is Hi
Item in List 1 is Hey
Item in List 2 is Hello
Total Items in List: 0

Finished!

Book traversal links for ArrayLists

  • ‹ Applets vs Applications
  • Up
  • Arrays ›

 

452,496

338,535

338,543

Top