package chapter.pkg7; import java.util.ArrayList; import java.util.Arrays; import javax.swing.JOptionPane; public class CommonArrayAlgorithms7_6 { private int[] values = new int[15]; private int valuesSize = 0; private ArrayList aList = new ArrayList (); private ArrayList numbers = new ArrayList (); public CommonArrayAlgorithms7_6() { numbers.add(11); numbers.add(9); numbers.add(5); numbers.add(1); numbers.add(101); numbers.add(2); numbers.add(9); numbers.add(7); numbers.add(5); numbers.add(102); numbers.add(11); numbers.add(9); numbers.add(11); numbers.add(7); numbers.add(103); numbers.add(3); numbers.add(5); numbers.add(11); numbers.add(1); numbers.add(104); numbers.add(12); numbers.add(13); numbers.add(14); numbers.add(15); numbers.add(105); } // Filling the array (or Arraylist) with values public void Filling() { for (int i = 0; i < 10; i++) { values[i] = (int)Math.pow(i, 3); valuesSize++; } // print array: //JOptionPane.showMessageDialog(null, Arrays.toString(values)); for (int i = 0; i < 10; i++) { aList.add(i * i); } // print ArrayList: //JOptionPane.showMessageDialog(null,aList); } public void sumAndAverage() { double total = 0; for (double element : values) { total = total + element; // total += element; } double average = total / valuesSize; JOptionPane.showMessageDialog(null,"total= " + total + " average= " + average); } // Counting Matches public void countMatches(int toMatch) { int matches = 0; for (Integer i : numbers) { if (i == toMatch) matches++; } JOptionPane.showMessageDialog(null,"Number of matches with " + toMatch + "= " + matches); } // Finding Maximum or Minimum public void findMaximum() { int num; int largestYet = numbers.get(0); for (int i = 1; i < numbers.size(); i++) { num = numbers.get(i); if (num > largestYet) { largestYet = num; } } JOptionPane.showMessageDialog(null,"largest= " + largestYet); } // Searching for a Value public boolean searchForAValue(Integer i) { boolean found = false; for (Integer hi : numbers) { if (hi == i) found = true; } JOptionPane.showMessageDialog(null,"Was " + i + " found? " + found); return found; } // Locating the Position of an Element public void locatePosition() { int pos = 0; boolean found = false; while (pos < numbers.size() && ! found) { if (numbers.get(pos) > 100) found = true; else pos++; } if (found) JOptionPane.showMessageDialog(null,"Position of first value >100 = " + pos); else JOptionPane.showMessageDialog(null,"Not found"); } // Removing an Element public void removeElement() { // easy with an ArrayList: numbers.remove(4); numbers.remove((Integer)101); //JOptionPane.showMessageDialog(null, numbers); // another possibility: ArrayList names = new ArrayList(); names.add("James"); names.remove("James"); int i; String msg = " "; // print contents of 'values': for (i = 0; i < valuesSize; i++) msg += values[i] + " "; JOptionPane.showMessageDialog(null, msg); //System.out.println(); // in an array, if the elements are NOT in a particular order, overwrite the element to be removed, // move the last element into that slot, and decrement the variable tracking the size of the array // remove the 7 from 'values' values[7] = values[valuesSize - 1]; valuesSize--; //int i; // print contents of 'values': msg = ""; for (i = 0; i < valuesSize; i++) msg += values[i] + " "; JOptionPane.showMessageDialog(null, msg); // with an array, if the elements ARE in a particular order, you have to move all the elements // that are affected. remove the element at position 3 from 'values': int numToRemove = 3; for (i = numToRemove; i < valuesSize; i++) { values[i] = values[i + 1]; } valuesSize--; // print contents of 'values': for (i = 0; i < valuesSize; i++) msg += values[i] + " "; JOptionPane.showMessageDialog(null, msg); } // Inserting an Element public void insertElement() { // with an ArrayList, use add() or set() JOptionPane.showMessageDialog(null,"aList: " + aList); aList.add(200); aList.add(5, 201); aList.set(11, 202); // print ArrayList: JOptionPane.showMessageDialog(null,"aList: " + aList); // print array: JOptionPane.showMessageDialog(null, "values: " + Arrays.toString(values)); // with an array, you have to move all the interfering elements out of the way first // insert a new element at position 'pos' int pos = 3; if (valuesSize < values.length) { for (int i = valuesSize; i > pos; i--) values[i] = values[i - 1]; values[pos] = 123; valuesSize++; } // print array: JOptionPane.showMessageDialog(null, "values: " + Arrays.toString(values)); } public static void main(String[] args) { CommonArrayAlgorithms7_6 c = new CommonArrayAlgorithms7_6(); c.Filling(); //c.sumAndAverage(); //c.countMatches(11); //c.findMaximum(); //c.searchForAValue(11); //c.locatePosition(); //c.removeElement(); c.insertElement(); } }