ArrayList
- ArrayList Definition
- Add and addAll
- Size
- Clear
- Remove (int index) and (element)
- Get
- Set
- IndexOf
- lastIndexOf(element)
- equals(element)
- hashCode()
- isEmpty
- contains(element)
- Sort
import java.util.ArrayList;
ArrayList<String> words = new ArrayList<String>();
words.add("Riya");
words.add("and");
words.add("Vidhi");
words.add("are");
words.add("cool");
System.out.println(words);
words.add("!"); //adds the ! to the end of the arraylist
words.add(4, "very"); //adds the word very to the list at index 4
System.out.println(words);
ArrayList<String> morewords = new ArrayList<String>();
morewords.add("Riya");
morewords.add("is");
morewords.add("cooler");
morewords.add("than");
morewords.add("Vidhi"); //this is very true
words.addAll(morewords); //adds the second list to the first one
System.out.println(words);
words.size(); //this is the new list words after using addAll()
words.clear(); //clears all the elements in list
System.out.println(words); //they're all gone, how sad
words.remove(3); //you can see that the word "are" has been removed
System.out.println(words);
words.remove("cooler");
words.remove("than");
System.out.println(words);
words.get(5);
words.set(3, new String("lol"));
System.out.println(words);
words.indexOf("csa");
words.lastIndexOf("Riya");
words.equals("hello");
words.hashCode();
words.isEmpty();
morewords.isEmpty();
words.contains("Vidhi");
words.contains("hello");
import java.util.Collections;
System.out.println("List : "+words);
//will sort the string according to alphabetical order
Collections.sort(words);
System.out.println("Sorted List : "+words);