Skip to main content

Data Structure

October 24, 2019   

ArrayList


// the data type must be a type of class
ArrayList<String>  a = new ArrayList<String>();
ArrayList<Integer> b = new ArrayList<Integer>();

// the size of the ArrayList
arrayList.size();

// add data to the ArrayList
arrayList.add("DATA");

// replace data by returning the value of the replaced data
arrayList.set();

// remove data by returning the value of the removed data
arratList.remove();

How to traverse the ArrayList

for(int i=0;i<arrayList.size();i++)
{
    int tmp = arrayList.get(i);
}

for(String tmp: words)
// FORM: for (dataType tmp:dataStructureName)

How to remove specific elements from ArrayList

// notice that the order is inverse
for(int i=arrayList.size()-1;i>=0;i--)
{
    int tmp = arrayList.get(i);
}

Stack


Stack<Integer> stack = new Stack<Integer>();
// inherit all the method from ArrayList
// new method like push,pop and peek
stack.push();
stack.pop();
// find the value of the top of the stack without removing it 
stack.peek();