• No results found

Arrays

N/A
N/A
Protected

Academic year: 2020

Share "Arrays"

Copied!
77
0
0

Loading.... (view fulltext now)

Full text

(1)

Arrays

Arrays

Multidimensional Arrays

(2)

Array Declaration

int[] someArray;

Declares an integer array

Brackets after the type = preferred

int someArray[];

Declares an integer array

Brackets after the variable name = not preferred

int someArray = new int[10];

Declares an array of size 10

Primitive types are initialized to their default values

int[] someArray = {1,2,3,4,5};

Declares and initializes an array for five integers

(3)

Default Values

int = 0

Boolean = false

Char = \u0000

Objects = null

(4)

Length of Array

int someArray[] = new int[10];

someArray.length returns 10

(5)

Array Example

int someArray[] = new int[10];

0 0 0 0 0 0 0 0 0 0

Index 0 1 2 3 4 5 6 7 8 9

Length 10

(6)

Array Example

int someArray[] = new int[10];

0 0 0 0 0 0 0 0 0 0

Index 0 1 2 3 4 5 6 7 8 9

Length 10

6

(7)

Array Example

int someArray[] = new int[10];

0 0 0 0 0 0 0 0 0 0

Index 0 1 2 3 4 5 6 7 8 9

Length 10

7

(8)

Array Example

int someArray[] = new int[10];

0 0 0 0 0 0 0 0 0 0

Index 0 1 2 3 4 5 6 7 8 9

Length 10

8

(9)

Indices

int someArray[] = new int[10];

0 0 0 0 0 0 0 0 0 0

Index 0 1 2 3 4 5 6 7 8 9

Length 10

9

(10)

Indices

int someArray[] = new int[10];

0 5 0 0 0 0 0 0 0 0

Index 0 1 2 3 4 5 6 7 8 9

Length 10

10

(11)

Indices

int someArray[] = new int[10];

0 5 5 0 0 0 0 0 0 0

Index 0 1 2 3 4 5 6 7 8 9

Length 10

11

(12)

Indices

int someArray[] = new int[10];

0 5 5 10 0 0 0 0 0 0

Index 0 1 2 3 4 5 6 7 8 9

Length 10

12

(13)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

(14)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5];

for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 14 Index Value 0 0 1 0 2 0 3 0 4 0 Array

(15)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 15 Index Value 0 0 1 0 2 0 3 0 4 0 Array

(16)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 16 Index Value 0 0 1 0 2 0 3 0 4 0 Array

(17)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1];

}

values[0] = values[1] + values[4]; } } 17 Index Value 0 0 1 1 2 0 3 0 4 0 Array

(18)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 18 Index Value 0 0 1 1 2 0 3 0 4 0 Array

(19)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 19 Index Value 0 0 1 1 2 0 3 0 4 0 Array

(20)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1];

}

values[0] = values[1] + values[4]; } } 20 Index Value 0 0 1 1 2 3 3 0 4 0 Array

(21)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 21 Index Value 0 0 1 1 2 3 3 0 4 0 Array

(22)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 22 Index Value 0 0 1 1 2 3 3 0 4 0 Array

(23)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1];

}

values[0] = values[1] + values[4]; } } 23 Index Value 0 0 1 1 2 3 3 6 4 0 Array

(24)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 24 Index Value 0 0 1 1 2 3 3 6 4 0 Array

(25)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 25 Index Value 0 0 1 1 2 3 3 6 4 0 Array

(26)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1];

}

values[0] = values[1] + values[4]; } } 26 Index Value 0 0 1 1 2 3 3 6 4 10 Array

(27)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 27 Index Value 0 0 1 1 2 3 3 6 4 0 Array

(28)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4]; } } 28 Index Value 0 0 1 1 2 3 3 6 4 0 Array

(29)

Simple Array Example

public class Test {

public static void main(String[] args) {

int[] values = new int[5]; for (int i = 1; i < 5; i++) {

values[i] = i + values[i-1]; }

values[0] = values[1] + values[4];

} } 29 Index Value 0 11 1 1 2 3 3 6 4 10 Array

(30)

Useful Things to do with Arrays

Print all elements

Sum all elements

Find the largest element

Shift elements

(31)

Print All Elements

for (int i = 0; i < myArray.length; i++)

{

System.out.print(myArray[i] + " ");

}

(32)

Sum All Elements

double total = 0;

for (int i = 0; i < myArray.length; i++)

{

total += myArray[i];

}

(33)

Find Largest Element

//should check that there is at least one element

double max = myArray[0];

for (int i = 1; i < myArray.length; i++)

{

if (myArray[i] > max)

{

max = myArray[i];

}

}

(34)

Shifting Elements

double temp = myArray[0];

for(int i=0; i<myArray.length-1; i++)

{

myArray[i] = myArray[i+1];

}

myArray[myArray.length-1] = temp;

(35)

Random Shuffle

For each array element

Choose random number

Swap array[i] and array[randomNumber]

(36)

Random Shuffle

//initialize array up here

Random random = new Random(0);

for(int i=0; i<myArray.length; i++)

{

int randomSwap = random.nextInt(myArray.length);

int temp = myArray[i];

myArray[i] = myArray[randomSwap];

myArray[randomSwap] = temp;

}

(37)

Example – RandomShuffle.java

RandomShuffle.java

(38)

Iterate Over All Elements

Iterate through all elements in order

Different type of for loop syntax

for(double value: myArray)

{

System.out.println(value);

}

(39)

Iterate Over All Elements

Iterate through all elements in order

Different type of for loop syntax

for(double value: myArray)

{

System.out.println(value);

}

39

(40)

Iterate Over All Elements

Iterate through all elements in order

Different type of for loop syntax

for(double value: myArray)

{

System.out.println(value);

}

40

(41)

Iterate Over All Elements

Iterate through all elements in order

Different type of for loop syntax

for(double value: myArray)

{

System.out.println(value);

}

41

(42)

Copying Arrays

42

Array Contents

array1

Array Contents

array2

(43)

Shallow Copy

43

Array Contents

array1

Array Contents

array2

array2 = array1;

Nothing pointing to array2

array2 = garbage

(44)

Deep Copy

44

Array Contents

array1

Array Contents

array2

for(int i=0; i<array1.length; i++) {

array2[i] = array1[i]; }

(45)

Arraycopy

System.arraycopy

https://

docs.oracle.com/javase/8/docs/api/java/lang/Syste

m.html#arraycopy-java.lang.Object-int-java.lang.

Object-int-int-–

public static void arraycopy

• (Object src, int srcPos, Object dest, int destPos, int length)

Arrays.copyOf

https://

docs.oracle.com/javase/8/docs/api/java/util/Arrays.

html

(46)

Searching Arrays

Linear Search

For unsorted arrays

Binary Search

For sorted arrays

Arrays.binarySearch

https://

docs.oracle.com/javase/8/docs/api/java/util/Arrays.ht

ml

(47)

Example – SearchExamples.java

SearchExamples.java

(48)

Sorting Arrays

Arrays.sort

https://

docs.oracle.com/javase/8/docs/api/java/util/Arrays.

html

Uses dual pivot quicksort for primitive types

Uses Timsort for Objects

Merge sort + Insertion sort hybrid

(49)

Example – SortComparison.java

SortComparison.java

(50)

Problem

Given an unsorted array of numbers determine

if the array contains a given value for each

value in a large list

Do not modify the original array

It is important not to lose the original data

(51)

Problem

What needs to be done?

Can do linear search for each value

Will be slow for a large number of values

Can do binary search for each value

Requires the list to be sorted

(52)

Possible Solution #1

Copy the original array

Sort the copied array

Search for each value in the copied array

(53)

Possible Solution #2

Copy the array

Sort the copied array

Copy the list of values

Sort the copied list of values

Use binary search

Change the minimum starting element based on

the results of the previous search

(54)

Example – ValueSearch.java

ValueSearch.java

Solution for solution #1

(55)

Multidimensional Arrays

(56)

Single Array

Can be thought of as a list

{1, 2, 3, 4, 5,}

1

2

3

4

5

(57)

2D Array

Can be thought of as a table

x

x

2

x

3

x

4

x

5

1

1

1

1

1

2

4

8

16

32

3

9

27

81

243

4

16

64

256

1024

5

25

125

625

3125

6

36

216

1296

7776

7

49

343

2401

16807

8

64

512

4096

32768

9

81

729

6561

59049

10

100 1000 10000 100000

(58)

N-dimensional array

How to visualize this?

Picture an array or rectangles

And rectangles inside each of those rectangles

And rectangles inside each of those rectangles

– Etc…

Treemap

(59)

59

One-dimensional array

One Dimensional Array (int[] x = new int[3]) x[1]

(60)

60

Two Dimensional Array (int[][] x = new int[3][3])

x[0] x[1] x[2]

x[1][1]

x[1][2]

x[2][1]

x[2][2]

x[1][0] x[2][0]

x[0][1]

x[0][2] x[0][0]

(61)

61

Three-dimensional array

x[0][1][2]

x[0][0] x[1][0] x[2][0]

x[0] x[1] x[2]

Three Dimensional Array (int[][][] x = new int[3][3][3])

(62)

Example – SpaceSniffer.exe

SpaceSniffer.exe

Program to visualize what contents are using

your disk space

Another example

https://

developers.google.com/chart/interactive/docs/galle

ry/treemap#example

(63)

Declaring and Creating

Two-dimensional Arrays

// Declare array ref var

dataType[][] refVar;

// Create array and assign its reference to variable

refVar = new dataType[10][10];

// Combine declaration and creation in one statement

dataType[][] refVar = new dataType[10][10];

// Alternative syntax

dataType refVar[][] = new dataType[10][10];

(64)

Two-Dimensional Array Example

int[][] array = new int[5][5];

64

0

1

2

3

4

0

0

0

0

0

0

1

0

0

0

0

0

2

0

0

0

0

0

3

0

0

0

0

0

(65)

Two-Dimensional Array Example

array[1][4] = 222;

65

0

1

2

3

4

0

0

0

0

0

0

1

0

0

0

0

222

2

0

0

0

0

0

3

0

0

0

0

0

(66)

Two-Dimensional Array Example

int[][] array = new int[5][];

66

(67)

Two-Dimensional Array Example

array[0] = new int[5];

67

0

1

2

3

4

0

0

0

0

0

0

(68)

Two-Dimensional Array Example

array[1] = new int[4];

68

0

1

2

3

4

0

0

0

0

0

0

1

0

0

0

0

(69)

Two-Dimensional Array Example

array[2] = {1, 2, 3};

69

0

1

2

3

4

0

0

0

0

0

0

1

0

0

0

0

2

1

2

3

(70)

Two-Dimensional Array Example

int[][] array = {{1, 2}, {3, 4}, {5, 6, 7}}

70

0

1

2

0

1

2

1

3

4

(71)

Useful Things to do with

multi-dimensional Arrays

Print all elements

Sum all elements

Find the largest element

Shift elements

(72)

Print All Elements

72

for (int i = 0; i < myArray.length; i++)

{

for(int j=0; j<myArray[i].length; j++)

{

System.out.print(myArray[i][j] + " ");

}

(73)

Sum All Elements

73

double total = 0;

for (int i = 0; i < myArray.length; i++)

{

for(int j=0; j<myArray[i].length; j++)

{

total += myArray[i][j];

}

(74)

Find Largest Element

double max = myArray[0][0];

for (int i = 0; i < myArray.length; i++)

{

for(int j=0; j<myArray[i].length; j++)

{

if (myArray[i][j] > max)

{

max = myArray[i][j];

}

}

}

(75)

Shifting Elements

double temp = myArray[0][0];

for(int i=0; i<myArray.length-1; i++)

{

double temp2 = myArray[i][0];

for(int j=0; j<myArray[i].length-1; j++)

{

myArray[i][j] = myArray[i][j+1];

}

myArray[i][myArray[i].length-1] = temp2;

}

myArray[myArray.length-1][myArray[i].length-1] = temp;

(76)

Random Shuffle 2D Array

Need to loop through all elements

Need to choose a random position to swap with

(77)

Random Shuffle 2D Array

//initialize array and “random” variable up here

for(int i=0; i<myArray.length; i++)

{

for(int j=0; j<myArray[i].length; j++)

{

int randomI = random.nextInt(myArray.length);

int randomJ = random.nextInt(myArray.length);

int temp = myArray[i][j];

myArray[i][j] = myArray[randomI][randomJ];

myArray[randomI][randomJ] = temp;

}

}

https://docs.oracle.com/javase/8/docs/api/java/lang/Syste https://docs.oracle.com/javase/8/docs/api/java/util/Arrays. https://developers.google.com/chart/interactive/docs/galle

References

Related documents