package ch7;

import java.util.ArrayList;

import java.util.Arrays;

public class Ch7
{

public void workWithArrayList()
{ // ArrayLists grow and shrink dynamically,
// as a program runs
ArrayList list = new ArrayList ();
// add items:
// adding in this way puts the newly-added name to the end of the ArrayList
list.add("Matt");
list.add("Aneesh");
list.add("Devon");
list.add("Radwan");
System.out.println(list);
// adds Alan, removes nothing:
list.add(2, "Alan");
System.out.println(list);
// overwrites what is at position 2:
list.set(2, "Caroline");
System.out.println(list);
// 2 diffent ways to remove:
list.remove(0); // remove what is at a given position
System.out.println(list);
list.remove("Aneesh"); // remove a given entry
System.out.println(list);
System.out.println("size: " + list.size());
System.out.println("at position 0:" + list.get(0));
}

public void wrappers()
{ // An ArrayList can only store objects
// What do you do if you want to store integers
// in an ArrayList?
ArrayList mylist = new ArrayList(); // doesn't work
// int --> Integer
// double --> Double
// boolean --> Boolean
// Integer, Double and Boolean are "wrapper" classes
ArrayList mylist = new ArrayList(); // works
mylist.add(7); mylist.add(-23); mylist.add(14);
System.out.println("size of list: " + mylist.size());
System.out.println("first element: " + mylist.get(0));
}

public void foreachloop()
{ String[] names = {"Radwan", "Matt", "Aneesh","Alan","Devon","AJ","Shreyas","Shoumik","Dan","Caroline","Jaimie","Ramik","Jason","Josh"};
// using the "wrapper" class Integer to store integers in an ArrayList:
ArrayList numbers = new ArrayList();
for (int k = 1; k <= 10; k++)
    numbers.add(k);
// for-each loop
for (String name : names)
    System.out.println("Good afternoon, " + name);
// additional for-each loops:
for (Integer i : numbers)
    System.out.print(i + " ");
System.out.println();
for (Integer i : numbers)
    System.out.print(i * i + " ");
System.out.println();
for (Integer i : numbers)
    System.out.print(i * i * i + " ");
System.out.println();
}

public void partiallyFilledArrays()
{ // use an int (here "size") to keep track of how many of the slots in an array are being used
String[] names = new String[100];
int size = 0;
names[0] = "Chris"; size++;
names[1] = "Josh"; size++;
names[2] = "Caroline"; size++;
System.out.println("size: " + size);
System.out.println("length: " + names.length);
for (int k = 0; k < size; k++)
System.out.println(names[k]);
System.out.println("------------------------");
for (String m : names)
System.out.println(m);
}

public static void main(String[] args)
{
String name1 = "Shoumik ";
String name2 = "AJ ";
String name3 = "Aneesh ";
String[] names = new String[14500];
names[0] = "Caroline";
names[1] = "Matt ";
names[2] = "Shreyas";
// an alternative way of setting up an array:
int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};
String[] names2 = {"Josh", "Jason", "Ramik", "Alan"};
//System.out.println("names2 length: " + names2.length);
//System.out.println(names2[3]);
for (int k = 0; k < names2.length; k++)
{     System.out.print(names2[k] + " ");
}
// a shortcut for printing the contents of an array:
System.out.println(Arrays.toString(names));

Ch7 c = new Ch7();
//c.workWithArrayList();
//c.wrappers();
//c.foreachloop();
c.partiallyFilledArrays();
}
}