knowt logo

6. Arrays

/* 
Writer: SP
Date: April 1st
References: knowt & Fiveable
Unit 6: Arrays */

Arrays Intro

  • Arrays- A data structure that makes it easier to handle similar types of data.

    • It can store whether primitive or reference data types.

    • Arrays are also reference types

Making Arrays

  • There are two different ways to make arrays

    • A constructor method vs. A pre-initialized array.

Constructor Method:

dataType[] arrayName = new dataType[numberOfItems];
  • To make an array that hold 10 items, it will be: int[] Array = new int[10];

  • Default value of array

    Datatype

    Default Value

    int

    0

    double

    0

    boolean

    false

    reference

    null

Pre-initialized Arrays:

int[] arrayOne = {1,2,3,4,5,6};
  • We initialized an array of 6 integers.

Accessing elements

  • code to access elements: arrayName[index]

  • Java is a zero-indexed language, thus its index starts at 0.

  • To define the length of array, we use arrayName.length.

    • It is not index thingy, so it will return the correct number.

    • Want to access the last element using .length, you should write: ;

      • arrayName.length - 1

  • ArrayIndexOutOfBoundsException:

    • When we use an index outside the allowed range.

Traversing Arrays:

  • To access every value in the array.

  1. Basic Traversing

// Traversing all elements using for loop and start at index 0. 

int[] list = new int[5];
for (int i = 0; i < list.length; i++) {
condition;
}
// Traversing all elements using while loop and start at index 0. 

int[] list = new int[5];
int i = 0;

while (i<list.length) {
condition;
i++;
}
  1. Reverse Traversing

int[] arr = new int[5];
for (int i = arr.length-1; i<=0; i—-) {
condition;
}

int index = arr.length-1;
while (index<=0) {
condition;
i—-;
}

Enhanced For Loop

  • Enhanced for loop can be used to traverse through most data structures.

    • It cannot reverse.

  • Structure:

for (dataType i : arrayName) { 
do something with i;
}
  • We cannot directly change the value of elements using enhanced for loop because ‘i’ is just copy of element in the array.

  • However, we can use mutator methods to set the value.

/** 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 all students' names
*/
public static void doubleArray(Student[] array, String defaultName) {
for (Student student: array) {
student.setName(defaultName); // Sets each student's name to a default name
}
}

Developing Algorithms

  1. Reverse the array

public class ReverseArray {
public static void main(String[] args) {
// Initializing an array
int[] originalArray = {1, 2, 3, 4, 5};
int length = originalArray.length;

// Printing the original array
System.out.println("Original Array:");
for (int i = 0; i < length; i++) {
System.out.print(originalArray[i] + " ");
}

// Reversing the array
for (int i = 0; i < length / 2; i++) {
int temp = originalArray[i];
originalArray[i] = originalArray[length - 1 - i];
originalArray[length - 1 - i] = temp;
}

// Printing the reversed array
System.out.println("\nReversed Array:");
for (int i = 0; i < length; i++) {
System.out.print(originalArray[i] + " ");
}
}
}
  1. Finding Max in array of double values (Comparing with the first index value):

private double findMax(double[] v) {
double max = v[0];
for (int i = 1; index < v.length; i++) {
if (v[i] > max) {
max = v[i];
}
}
return max
}
  1. Finding Min value in int array (Comparing with the absolute maximum value):

private int findMin(int[] v) { 
int min = Integer.MAX_VALUE;
for (int currentValue : v) {
if (currentValue < min) {
min = currentValue;
}
}
return min;
}
  1. Calculate the average value of array

private double findMean(double[] list) { 
double sum = 0.0;
for (int i : list) {
sum += i;
}
double mean = sum/list.length;
return mean;
}
  1. Doing a right shift.

5. Left Shift

6. Finding Duplicate

for (int x = 0; x < Array.length; x++) { 
for (int y = 0; y< Array.length; y++) {
if (array[x] == array[x] && x!=y) {
// Duplicate statement
}
}
}

SS

6. Arrays

/* 
Writer: SP
Date: April 1st
References: knowt & Fiveable
Unit 6: Arrays */

Arrays Intro

  • Arrays- A data structure that makes it easier to handle similar types of data.

    • It can store whether primitive or reference data types.

    • Arrays are also reference types

Making Arrays

  • There are two different ways to make arrays

    • A constructor method vs. A pre-initialized array.

Constructor Method:

dataType[] arrayName = new dataType[numberOfItems];
  • To make an array that hold 10 items, it will be: int[] Array = new int[10];

  • Default value of array

    Datatype

    Default Value

    int

    0

    double

    0

    boolean

    false

    reference

    null

Pre-initialized Arrays:

int[] arrayOne = {1,2,3,4,5,6};
  • We initialized an array of 6 integers.

Accessing elements

  • code to access elements: arrayName[index]

  • Java is a zero-indexed language, thus its index starts at 0.

  • To define the length of array, we use arrayName.length.

    • It is not index thingy, so it will return the correct number.

    • Want to access the last element using .length, you should write: ;

      • arrayName.length - 1

  • ArrayIndexOutOfBoundsException:

    • When we use an index outside the allowed range.

Traversing Arrays:

  • To access every value in the array.

  1. Basic Traversing

// Traversing all elements using for loop and start at index 0. 

int[] list = new int[5];
for (int i = 0; i < list.length; i++) {
condition;
}
// Traversing all elements using while loop and start at index 0. 

int[] list = new int[5];
int i = 0;

while (i<list.length) {
condition;
i++;
}
  1. Reverse Traversing

int[] arr = new int[5];
for (int i = arr.length-1; i<=0; i—-) {
condition;
}

int index = arr.length-1;
while (index<=0) {
condition;
i—-;
}

Enhanced For Loop

  • Enhanced for loop can be used to traverse through most data structures.

    • It cannot reverse.

  • Structure:

for (dataType i : arrayName) { 
do something with i;
}
  • We cannot directly change the value of elements using enhanced for loop because ‘i’ is just copy of element in the array.

  • However, we can use mutator methods to set the value.

/** 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 all students' names
*/
public static void doubleArray(Student[] array, String defaultName) {
for (Student student: array) {
student.setName(defaultName); // Sets each student's name to a default name
}
}

Developing Algorithms

  1. Reverse the array

public class ReverseArray {
public static void main(String[] args) {
// Initializing an array
int[] originalArray = {1, 2, 3, 4, 5};
int length = originalArray.length;

// Printing the original array
System.out.println("Original Array:");
for (int i = 0; i < length; i++) {
System.out.print(originalArray[i] + " ");
}

// Reversing the array
for (int i = 0; i < length / 2; i++) {
int temp = originalArray[i];
originalArray[i] = originalArray[length - 1 - i];
originalArray[length - 1 - i] = temp;
}

// Printing the reversed array
System.out.println("\nReversed Array:");
for (int i = 0; i < length; i++) {
System.out.print(originalArray[i] + " ");
}
}
}
  1. Finding Max in array of double values (Comparing with the first index value):

private double findMax(double[] v) {
double max = v[0];
for (int i = 1; index < v.length; i++) {
if (v[i] > max) {
max = v[i];
}
}
return max
}
  1. Finding Min value in int array (Comparing with the absolute maximum value):

private int findMin(int[] v) { 
int min = Integer.MAX_VALUE;
for (int currentValue : v) {
if (currentValue < min) {
min = currentValue;
}
}
return min;
}
  1. Calculate the average value of array

private double findMean(double[] list) { 
double sum = 0.0;
for (int i : list) {
sum += i;
}
double mean = sum/list.length;
return mean;
}
  1. Doing a right shift.

5. Left Shift

6. Finding Duplicate

for (int x = 0; x < Array.length; x++) { 
for (int y = 0; y< Array.length; y++) {
if (array[x] == array[x] && x!=y) {
// Duplicate statement
}
}
}

robot