12 min readβ’december 31, 2022
Peter Cao
Milo Chang
int[][] arrayB = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
for (firstDimension traversal conditions) {
for (secondDimension traversal conditions) {
System.out.print(item + " ");
}
}
System.out.println();
Row-wise Traversal
Column-wise Traversal
Row-Wise Traversal
public static void rowWiseForward(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
System.out.print(array[i][j] + " ");
}
}
}
public static void main(str[] args) {
rowWiseForward(arrayB);
}
1 2 3 4 5 6 7 8 9 10 11 12
public static void rowWiseForwardEnhancedForLoops(int[][] array) {
for (int[] row: array) {
for (int number: row) {
System.out.print(number + " ");
}
}
}
Column-Wise Traversal
public static void columnWiseForward(int[][] array) {
for (int i = 0; i < array[0].length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[j][i] + " ");
}
}
}
public static void main(str[] args) {
columnWiseForward(arrayB);
}
1 5 9 2 6 10 3 7 11 4 8 12
In Summary
i < array.length
;i < array[0].length
;int i = array.length() - 1; i >= 0; i--
int i = array[0].length - 1; i >= 0; i--
1. 1 2 3 4 8 7 6 5 9 10 11 12
2. 12 8 4 3 7 11 10 6 2 1 5 9
public static void exampleOne(int[][] array) {
for (int i = 0; i < array.length; i++) {
if (i % 2 == 0) { // we use an if statement with modulo base 2 to alternate
for (int j = 0; j < array[0].length; j++) {
System.out.print(array[i][j] + " ");
}
} else {
for (int j = array[0].length - 1; j >= 0; j--) {
System.out.print(array[i][j] + " ");
}
}
}
}
public static void exampleTwo(int[][] array) {
for (int i = array[0].length - 1; i >= 0; i--) {
if (i % 2 == 0) { // we use an if statement with modulo base 2 to alternate
for (int j = 0; j < array[0].length - 1; j++) {
System.out.print(array[j][i] + " ");
}
} else {
for (int j = array.length - 1; j >= 0; j--) {
System.out.print(array[j][i] + " ");
}
}
}
}
Sequential/Linear Search
/** Prints the row and column indices if the element is in the array and -1 if not
*/
public static boolean searchForElement(int[][] array, int elementToSearch) {
flag = false; //sets flag to see if element has been found
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) { //traverses through the array
if (array[i][j] == elementToSearch) {
System.out.println("Row " + i);
System.out.println("Column " + j); //if element found, print coordinates
return true; //element has been found
}
}
}
if (!flag) { //if element not found, return false
return false;
}
}
Modifying Array Values
/** Doubles each element of the array
*/
public static void doubleArray(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
array[i][j] *= 2; // doubles each individual element
}
}
}
/** Doubles each element of the array
*/
public static void doubleArray(int[][] array) {
for (int[] row: array) {
for (int number: row) {
number *= 2; // doubles number
}
}
}
/** Represents a student
*/
public class Student {
private String name;
/** Sets the name of the Student
*/
public void setName(String name) {
this.name = name;
}
/** Other instance variables, methods, and constructors not shown
*/
}
// IN ANOTHER CLASS
/** Resets names of all student s
*/
public static void doubleArray(Student[][] array, String defaultName) {
for (Student[] row: array) {
for (Student student: row) {
student.setName(defaultName); // Sets each student's name to a default name
}
}
}
Putting all the Values Into an ArrayList
/** Inserts all the values in the array into an ArrayList
*/
public static ArrayList<Integer> putIntoArrayList(int[][] array) {
ArrayList<Integer> intList = new ArrayList<Integer>(); //makes an empty ArrayList
for (int[] row: array) {
for (int number: row) {
//Boxes the integer to an Integer object adding it to the ArrayList
intList.add(new Integer(number));
}
}
return intList;
}
intList.add(number);
and Java will automatically convert the integer into an Integer object. Putting all the Values Into an Array
/** Inserts all the values in the array into an array
*/
public static int[] putIntoArray(int[][] array) {
int[] intArray = new int[array.length * array[0].length]; //initialize the array
for (int i = 0; i < array.length; i++) { //to the number of items in rect array
for (int j = 0; j < array[0].length; j++) {
//i*array[0].length + j is the nth item
intArray[i*array[0].length + j] = array[i][j];
}
}
return intArray;
}
Finding the Minimum and Maximum
/** Finds the maximum
*/
public static int maximum(int[][] array) {
int maxValue = array[0][0];
for (int[] row: array) {
for (int number: row) {
if (number > maxValue) { //if new max value found, replace current maxValue
maxValue = number;
}
}
}
return maxValue;
}
/** Finds the minimum
*/
public static int minimum(int[][] array) {
int minValue = array[0][0];
for (int[] row: array) {
for (int number: row) {
if (number < minValue) { //if new min value found, replace current minValue
minValue = number;
}
}
}
return minValue;
}
Finding a Sum
/** Sums up all elements in the array
*/
public static int sum(int[][] array) {
int sum = 0;
for (int[] row: array) {
for (int number: row) {
sum += number; //adds every element to sum
}
}
return sum;
}
Finding a Mean
/** Finds the mean/average of the array
*/
public static int mean(int[][] array) {
// find the sum of the array, can be replaced with sum algorithm above
int sum = sum(array);
return (double) sum / (array.length * array[0].length);
}
Finding a Mode
/** Finds the mode of an array
Prerequisite:
The array must have a mode
*/
public static int mode(int[][] array) {
int[] newArray = putIntoArray(array) //places the numbers into a 1D array as above
int mostCommon = 0;
int mostCommonFrequency = 0;
for (int i = 0; i < newArray.length - 1; i++) { //traverse through the new array
int currentFrequency = 1;
for (int j = i + 1; j < newArray.length; j++) { //traverse through rest of array
// if any element matches current element being checked, add 1 to frequency
if (newArray[j] == newArray[i]) {
currentFrequency++;
}
}
if (currentFrequency > mostCommonFrequency) {
mostCommon = newArray[i]; // replaces current mode if new most common element
mostCommonFrequency = currentFrequency;
}
}
return mostCommon; // can also be modified to return the frequency
}
Determining If All Values Have a Certain Property
/** Determines whether all values are even
*/
public static boolean isEven(int[][] array) {
//Assume all values are positive first
for (int[] row: array) {
for (int number: row) {
if (number % 2 == 1) { //If there is one value that is not positive, return false
return false;
}
}
}
return true; //No odd numbers were found
}
Accessing All Consecutive Pairs/Triplets/Sequences of Length n of Elements
/** Returns all consecutive sequences of length n in the array
*/
public static void returnAllConsecutiveSequences(int[][] array, int length) {
public int[] oneDArray = putIntoArray(array);
for (int i = 0; i <= oneDArray.length - length; i++) {
for (int j = 0; j < length; j++) {
System.out.print(oneDArray[i+j] + " "); //2 loops, one to get the starting number
} //the other to go through the sequences
System.out.println();
}
}
Checking if There are Duplicate Elements
/** Checks to see if there are duplicate elements
*/
public static boolean duplicates(int[][] array) {
int[] newArray = putIntoArray(array) //places the numbers into a 1D array as above
for (int i = 0; i < newArray.length - 1; i++) { //traverse through the new array
for (int j = i + 1; j < newArray.length; j++) { //traverse through rest of array
// if any element matches current element being checked, return true
if (newArray[j] == newArray[i]) {
return true;
}
}
}
return false; // if this point reached, no duplicates found
}
Determining How Many Elements Fit a Criteria
/** Returns how many even numbers there are
*/
public static int evenFrequency(int[][] array) {
int numberEven = 0;
for (int[] row: array) {
for (int number: row) {
if (number % 2 == 0) {
numberEven++; // increments every time an even integer is found
}
}
}
return numberEven;
}
Β© 2023 Fiveable Inc. All rights reserved.