Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. Janyl - - PowerPoint PPT Presentation

data abstraction copying arrays sorting arrays 2d arrays
SMART_READER_LITE
LIVE PREVIEW

Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. Janyl - - PowerPoint PPT Presentation

Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. Janyl Jumadinova September 30 and October 5, 2020 Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 1 / 15 Copying Arrays


slide-1
SLIDE 1

Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays.

Janyl Jumadinova September 30 and October 5, 2020

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 1 / 15

slide-2
SLIDE 2

Copying Arrays

=: Copying only the reference - only have one actual set of contents! System.arraycopy method: Copy data array beginning at specified position to the specified position of the copy array. System.arraycopy(data, 0, copy, 0, data.length);

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 2 / 15

slide-3
SLIDE 3

Copying Arrays

clone method: Object class’s method: new array can reference the same elements as the

  • riginal array.

Double[] copy = data.clone();

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 3 / 15

slide-4
SLIDE 4

Copying Arrays

clone method: Object class’s method: new array can reference the same elements as the

  • riginal array.

Double[] copy = data.clone(); Arrays.copyOf method: Copy contents of one array to another one Double[] copy = Arrays.copyOf(data, data.length);

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 3 / 15

slide-5
SLIDE 5

Copying Arrays

clone method: Object class’s method: new array can reference the same elements as the

  • riginal array.

Double[] copy = data.clone(); Arrays.copyOf method: Copy contents of one array to another one Double[] copy = Arrays.copyOf(data, data.length); These methods perform a shallow copy of the array.

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 3 / 15

slide-6
SLIDE 6

Copying Arrays

Car c = new Car("Ford", 2000); Shallow Copy:

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 4 / 15

slide-7
SLIDE 7

Copying Arrays

Car c = new Car("Ford", 2000); Shallow Copy: For some Java classes shallow copy behaves the same as the deep copy because those classes are immutable - instance can not be changed once created (e.g., String class, wrapper classes).

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 4 / 15

slide-8
SLIDE 8

Copying Arrays

Car c = new Car("Ford", 2000); Deep Copy: Original and copy are totally distinct. copy = new int[data.length]; for (int i = 0; i < data.length; i++) copy[i] = data[i];

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 5 / 15

slide-9
SLIDE 9

Enhanced For Loop

double[] data = ... ; double sum = 0; for (double element : data) { sum += element; } Access each element in order (0 to length-1) Copy it to the element variable Execute loop body

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 6 / 15

slide-10
SLIDE 10

The Sorting Problem

Input: A sequence of n items, a1, a2, ..., an

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 7 / 15

slide-11
SLIDE 11

The Sorting Problem

Input: A sequence of n items, a1, a2, ..., an Output: Reordering of the input sequence so that a′

1 ≤ a′ 2 ≤ ...a′ n

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 7 / 15

slide-12
SLIDE 12

Insertion Sort

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 8 / 15

slide-13
SLIDE 13

Insertion Sort

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 9 / 15

slide-14
SLIDE 14

Bubble Sort

Idea: Repeatedly pass through the array Swap adjacent elements that are out of order

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 10 / 15

slide-15
SLIDE 15

Bubble Sort

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 11 / 15

slide-16
SLIDE 16

Two Dimensional Arrays

A one-dimensional array stores a list of elements. A two-dimensional array can be thought of as a table of elements, with rows and columns.

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 12 / 15

slide-17
SLIDE 17

Two Dimensional Arrays

A two-dimensional array is an array of arrays. A two-dimensional array is declared by specifying the size of each dimension separately: int[][] scores = new int[12][50]; An array element is referenced using two index values: value = scores[3][6]; The array stored in one row can be specified using one index.

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 13 / 15

slide-18
SLIDE 18

Two Dimensional Arrays: Example

int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) { for (int col=0; col < table[row].length; col++) { table[row][col] = row * 10 + col; } } // Print the table for (int row=0; row < table.length; row++) { for (int col=0; col < table[row].length; col++) { System.out.print (table[row][col] + "\t"); } System.out.println(); }

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 14 / 15

slide-19
SLIDE 19

Two Dimensional Arrays: Example

Janyl Jumadinova Data Abstraction Copying Arrays. Sorting Arrays. 2D Arrays. September 30 and October 5, 2020 15 / 15