get Method
This documentation is archived and is not being maintained.

ArrayList.get Method

Visual Studio 2005

Returns the element at the specified index.

Package: java.util

Assembly: vjslib (in vjslib.dll)


public java.lang.Object get(
    int ix);

Parameters

ix

The index of the element to be returned.

The element at the specified index.

The following example creates an ArrayList containing various breeds of dogs. It then calls get passing both valid and invalid indices.

// arraylist_get.jsl

import java.util.*;

public class Program
{
    public static void main(String[] args)
    {
        // Fill an ArrayList with various breeds of dogs.
        ArrayList dogs = new ArrayList();

        Dog fido = new Dog("Golden Retriever");
        Dog fluffy = new Dog("Poodle");
        Dog shadow = new Dog("Basenji");
        Dog miki = new Dog("Yorkshire Terrier");

        dogs.add(fido);
        dogs.add(fluffy);
        dogs.add(shadow);
        dogs.add(miki);

        try
        {
            // Print out the dogs in the ArrayList.
            // oops, goes beyond the end of the ArrayList.
            for (int i = 0; i < 5; i++)
            {
                System.out.println(dogs.get(i).toString());
            }
        }
        catch (IndexOutOfBoundsException ex)
        {
            System.out.println("get was called with an illegal index");
        }
    }
}

public class Dog
{
    public Dog(String breed)
    {
        this.breed = breed;
    }

    public String toString()
    {
        return breed;
    }

    private String breed;
}

/*
Output:
Golden Retriever
Poodle
Baseni
Yorkshire Terrier
get was called with an illegal index
*/
Show:
© 2016 Microsoft