Array, ArrayList
-can change size one created? no, yes
-is a class? no, yes
-use methods to access and modify element? no, yes
-what can it store? primitive and objects, only objects
ArrayList
import java.util.*; public class ArrayListExample { public static void main(String args[]) { /*Creation of ArrayList: I'm going to add String *elements so I made it of string type */ ArrayList<String> obj = new ArrayList<String>(); /*This is how elements should be added to the array list*/ obj.add("Ajeet"); obj.add("Harry"); obj.add("Chaitanya"); obj.add("Steve"); obj.add("Anuj"); /* Displaying array list elements */ System.out.println("Currently the array list has following elements:"+obj); /*Add element at the given index*/ obj.add(0, "Rahul"); obj.add(1, "Justin"); /*Remove elements from array list like this*/ obj.remove("Chaitanya"); obj.remove("Harry"); System.out.println("Current array list is:"+obj); /*Remove element from the given index*/ obj.remove(1); System.out.println("Current array list is:"+obj); } }
NumberActivity.java
/.../ package com.example.android.miwok; public class NumbersActivity extends AppCompactActivity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_numbers); // Create on array of words ArrayList<String> words = new ArrayList<String>(); words.add("one"); words.add("two"); words.add("three"); words.add("four"); words.add("five"); words.add("six"); words.add("seven"); words.add("eight"); words.add("nine"); words.add("ten"); Log.v("NumbersActivity","Word at index 0: " + words.get(0)); Log.v("NumbersActivity","Word at index 1: " + words.get(1)); } }