import java.util.ArrayList;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.ArrayList;
public class CommonArrayAlgorithms7_6
{
public CommonArrayAlgorithms7_6()
// Filling the array (or Arraylist) with values
// print array:
// print ArrayList:
public void sumAndAverage()
// Counting Matches
// Finding Maximum or Minimum
// Searching for a Value
// Locating the Position of an Element
// Removing an Element
int i;
// in an array, if the elements are NOT in a particular order, overwrite the element to be removed,
// print contents of 'values':
// with an array, if the elements ARE in a particular order, you have to move all the elements
// print contents of 'values':
// Inserting an Element
// print ArrayList:
// print array:
// with an array, you have to move all the interfering elements out of the way first
// print array:
public static void main(String[] args)
private int[] values = new int[15];
private int valuesSize = 0;
private ArrayList
private ArrayList
{
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);
}
public void Filling()
{
for (int i = 0; i < 10; i++)
{
values[i] = (int)Math.pow(i, 3);
valuesSize++;
}
System.out.println(Arrays.toString(values));
for (int i = 0; i < 10; i++)
{
aList.add(i * i);
}
System.out.println(aList);
}
{
double total = 0;
for (double element : values)
{
total = total + element;
// total += element;
}
double average = total / valuesSize;
System.out.println("total= " + total + " average= " + average);
}
public void countMatches(int toMatch)
{
int matches = 0;
for (Integer i : numbers)
{
if (i == toMatch)
matches++;
}
System.out.println("Number of matches with " + toMatch + "= " + matches);
}
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;
}
}
System.out.println("largest= " + largestYet);
}
public boolean searchForAValue(Integer i)
{
boolean found = false;
for (Integer hi : numbers)
{
if (hi == i) found = true;
}
System.out.println("Was " + i + " found? " + found);
return found;
}
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)
System.out.println("Position of first value >100 = " + pos);
else
System.out.println("Not found");
}
public void removeElement()
{
// easy with an ArrayList:
numbers.remove(4);
// print contents of 'values':
for (i = 0; i < valuesSize; i++)
System.out.print(values[i] + " ");
System.out.println();
// 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;
for (i = 0; i < valuesSize; i++)
System.out.print(values[i] + " ");
System.out.println();
// 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--;
for (i = 0; i < valuesSize; i++)
System.out.print(values[i] + " ");
System.out.println();
}
public void insertElement()
{
// with an ArrayList, use add() or set()
aList.add(200);
aList.add(5, 201);
aList.set(11, 202);
System.out.println(aList);
int num;
for (int j = 0; j < valuesSize; j++)
{
num = values[j];
System.out.print(num + " ");
}
System.out.println();
// 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++;
}
for (int j = 0; j < valuesSize; j++)
{
num = values[j];
System.out.print(num + " ");
}
}
{
CommonArrayAlgorithms7_6 c = new CommonArrayAlgorithms7_6();
c.Filling();
//c.sumAndAverage();
//c.countMatches(11);
//c.findMaximum();
//c.searchForAValue(11);
//c.locatePosition();
//c.removeElement();
c.insertElement();
}
}