Unit 7: ArrayList

...back Table of Contents 

7.1 Introduction to ArrayList [sample]

7.2 ArrayList Methods

7.3 Traversing ArrayLists

7.4 Developing Algorithms Using ArrayLists

7.5 Searching

7.6 Sorting

7.7 Ethical Issues Around Data Collection


7.1 Introduction to ArrayList [sample]


import java.util.ArrayList;

public class Main

{

  static ArrayList<Integer> arrList = new ArrayList<Integer>();

  public static void main(String[] args)

  {

    arrList.add(10);

    arrList.add(2);

    arrList.add(3);

    arrList.add(4);

    arrList.add(5);

    arrList.add(6);

    arrList.add(7);

    arrList.add(8);

    arrList.add(9);

    arrList.add(1);

    arrList.add(10);

    arrList.add(11);

    arrList.add(13);

    arrList.add(12);



7.4 Developing Algorithms Using ArrayLists


import java.util.ArrayList;


public class STDArray

{

  static ArrayList<Integer> arrList = new ArrayList<Integer>();

 

  public static void main(String[] args)

  {

    arrList.add(1);

    arrList.add(2);

    arrList.add(0);

    arrList.add(3);

    arrList.add(2);

    arrList.add(4);

    arrList.add(2);

    arrList.add(1);

    arrList.add(0);

    arrList.add(2);

    arrList.add(0);

    arrList.add(1);

    arrList.add(3);

    arrList.add(2);


    System.out.println("SUM " + sum());

    System.out.println("AVE b " + ave());

    System.out.println("MAX " + max());

    System.out.println("MIN " + min());

    System.out.println("MODE " + mode());

    System.out.println("FREQ " + frequency());

    System.out.println("AT LEAST " + atLeastOne());

    System.out.println("ALL " + all());


    // arrList is modified with each of the following calls

    System.out.println("\nBefore PAIRS" + arrList);

    System.out.println("PAIRS " + pairs());

   

    System.out.println("\nBefore SHIFTLEFT" + arrList);

    System.out.println("SHIFTLEFT "shiftLeft());

   

    System.out.println("\nBefore REVERSE" + arrList);

    System.out.println("REVERSE "reverse());

  }

 

  public static int sum()

  {

    int sum = 0;

    for (int a : arrList )

      sum += a;

    return sum;

  }

 

  public static double ave()

  {

    double aveb = 0;

    for (int b : arrList )

      aveb += b;

    return aveb / arrList.size();

  }

 

  public static int max()

  {

    int max = arrList.get(0);

    for (int i = 1; i < arrList.size(); i++)

    {

      if (arrList.get(i) > max)

        max = arrList.get(i);

    }

    return max;

  }

 

  public static int min()

  {

    int min = arrList.get(0);

    for (int i = 1; i < arrList.size(); i++)

    {

      if (arrList.get(i) < min)

        min = arrList.get(i) ;

    }

    return min;

  }


  public static int mode()

  {

    int modeValue = 0;

    int mode = 0;

    ArrayList<Integer> modeCount = new ArrayList<Integer>();

    for (int a : arrList)

          modeCount.add(0);

    for (int a : arrList)

    {

      modeCount.set(a, ( modeCount.get(a) + 1));

      if ( modeCount.get(a) > modeValue)

      {

        modeValuemodeCount.get(a);

        mode = a;

      }

    }

    return mode;

  }

 

  public static int frequency()

  {

    int freq = 0;

    for (int a : arrList)

    {

      if (a == 0)

        freq++;

    }

    return freq;

  }

 

  public static boolean atLeastOne()

  {

    int i = 0;

    boolean hasValue = false;

    while (!hasValue && (i < arrList.size()))

    {

      if (arrList.get(i) == 0)

        hasValue = true;

      i++;

    }

    return hasValue;

  }

 

  public static boolean all()

  {

    boolean allHaveValue = true;

    for (int a : arrList)

    {

      if (a <= 0)

        allHaveValue = false;

    }

    return allHaveValue;

  }

 

  /*

   * postcondition: modifies arrList

   */

  public static ArrayList<Integer> pairs()

  {

    for (int i = 0; i < arrList.size() - 1; i += 2)

    {

      int swap = arrList.get(i);

      arrList.set(i, arrList.get(i+1));

      arrList.set((i+1), swap);

    }

    return arrList;

  }

 

  /*

   * postcondition: modifies arrList

   */

  public static ArrayList<Integer> shiftLeft()

  {

    for (int i = arrList.size(); i > 1; i--)

    {

      int shiftVal= arrList.get(i-2);

      arrList.set((i-2), arrList.get(i-1));

      arrList.set((i-1), shiftVal);

     

    }

    return arrList;

  }

 

  /*

   * postcondition: modifies arrList

   */

  public static ArrayList<Integer>reverse()

  {

    for (int i = 0; i < arrList.size()/2; i++)

    {

      int tmp = arrList.get(i);

      arrList.set(i, arrList.get(arrList.size() - i - 1));

      arrList.set((arrList.size() - i - 1), tmp);

    }

    return arrList;

  }

   

}