Friday, April 22, 2016

ArrayLists and Lists

Lists and ArrayLists in Java programming are objects that store other objects inside of them, they are very useful for storing data in multiple levels.
A usual object can store data inside of it as well as objects but ArrayLists keep the data organized in a deeper level. They are somewhat like normal arrays (String[]) however they are elastic instead of solid, meaning that they can increase and decrease in size depending on how much is required, instead of in a set amount.

public static void main(String[]args){

ArrayList<String> names = new ArrayList();// This is an example of instantiating an array list
List<String> moreNames = new ArrayList();//Lists and ArrayLists are similar however ArrayLists //are child objects from the List class. Thus the ArrayList class contains all of the previous methods //and more.

names.add("Joseph");//This is how to add a new object to an array list, it returns a boolean: true;
names.add("Smigla");
names.add(0, "Edward");//This adds a new object in the place of what ever is located in the index 
           //and moves the object in that index to the index higher than itself
           // The array will end up as: Edward, Joseph.
names.remove(1);//This removes an object from the array and and if it is larger than the index it //moves all objects after it downward an index unit, it also returns the object.

for(String n: names){System.out.print(n + " ");}//This will print in the console the objects in the list:
// Edward Smigla

}

3 comments:

  1. Wish I understood this they way you do.You tried to teach me coding the other day haha.

    ReplyDelete
  2. I really wish I knew what all of this is. Maybe I'll learn from reading this blog weekly.

    ReplyDelete