Title: Arrays Vectores
1Arrays (Vectores)
OutlineDeclaring ArraysCreating
ArraysInitializing ArraysManipulating Arrays
Passing Arrays to MethodsMultidimensional
Arrays
2Declaring and Creating Arrays
- Created dynamically with keyword new
- int c new int 12
- or
- int c new int 12
- Equivalent to int c // declare array
variable c new int 12 // create array - We can create arrays of objects too
- String b new String 100
3c 0
Name of array (note that all elements of this
array have the same name, c)
c 1
c 2
c 3
c 4
c 5
c 6
c 7
c 8
c 9
c 10
Index (or subscript) of the element in array c
c 11
Fig. 7.1 A 12-element array.
4Initializing Arrays
- Using an array initializer
- Use initializer list
- Items enclosed in braces ()
- Items in list separated by commas
- int n 10, 20, 30, 40, 50
- Creates a five-element array
- Index values of 0, 1, 2, 3, 4
- Do not need keyword new
5Manipulating Arrays
- Examine array c
- c is the array name
- c.length accesses array cs length
- c has 12 elements ( c0, c1, c11 )
- The value of c0 is 45
6- 1 // Fig. 7.2 InitArray.java
- 2 // Creating an array.
- 3 import javax.swing.
- 4
- 5 public class InitArray
- 6
- 7 public static void main( String args
) - 8
- 9 int array // declare
reference to an array - 10
- 11 array new int 10 // create
array (initialized with 0's by default) - 12
- 13 String output "Index\tValue\n"
- 14
- 15 // append each array element's value
to String output - 16 for ( int counter 0 counter lt
array.length counter ) - 17 output counter "\t" array
counter "\n" - 18
- 19 JTextArea outputArea new
JTextArea()
7- 1 // Fig. 7.3 InitArray.java
- 2 // Initializing an array with a
declaration. - 3 import javax.swing.
- 4
- 5 public class InitArray
- 6
- 7 public static void main( String args
) - 8
- 9 // array initializer specifies
number of elements and - 10 // value for each element
- 11 int array 32, 27, 64, 18, 95,
14, 90, 70, 60, 37 - 12
- 13 String output "Index\tValue\n"
- 14
- 15 // append each array element's value
to String output - 16 for ( int counter 0 counter lt
array.length counter ) - 17 output counter "\t" array
counter "\n" - 18
- 19 JTextArea outputArea new
JTextArea()
8- 1 // Fig. 7.4 InitArray.java
- 2 // Initialize array with the even integers
from 2 to 20. - 3 import javax.swing.
- 4
- 5 public class InitArray
- 6
- 7 public static void main( String args
) - 8
- 9 final int ARRAY_LENGTH 10 //
constant - 10 int array //
reference to int array - 11
- 12 array new int ARRAY_LENGTH //
create array - 13
- 14 // calculate value for each array
element - 15 for ( int counter 0 counter lt
array.length counter ) - 16 array counter 2 2
counter - 17
- 18 String output "Index\tValue\n"
- 19
9- 1 // Fig. 7.7 RollDie.java
- 2 // Roll a six-sided die 6000 times.
- 3 import javax.swing.
- 4
- 5 public class RollDie
- 6
- 7 public static void main( String args
) - 8
- 9 int frequency new int 7
- 10
- 11 // roll die 6000 times use die value
as frequency index - 12 for ( int roll 1 roll lt 6000
roll ) - 13 frequency 1 ( int ) (
Math.random() 6 ) - 14
- 15 String output "Face\tFrequency"
- 16
- 17 // append frequencies to String
output - 18 for ( int face 1 face lt
frequency.length face ) - 19 output "\n" face "\t"
frequency face
Increment frequency values at index associated
with random number
10Passing Arrays to Methods (1)
- Two ways to pass arguments to methods
- Pass-by-value
- Copy of arguments value is passed to called
method - In Java, every primitive is pass-by-value
- Pass-by-reference
- Caller gives called method direct access to
callers data - Called method can manipulate this data
- Improved performance over pass-by-value
- In Java, every object is pass-by-reference
- In Java, arrays are objects
- Therefore, arrays are passed to methods by
reference
11Passing Arrays to Methods (2)
- Example Sorting
- Bubble sort
- Smaller values bubble their way to top of array
- Larger values sink to bottom of array
- Use nested loops to make several passes through
array - Each pass compares successive pairs of elements
- Pairs are left along if increasing order (or
equal) - Pairs are swapped if decreasing order
12- 1 // Fig. 7.10 BubbleSort.java
- 2 // Sort an array's values into ascending
order. - 3 import java.awt.
- 4 import javax.swing.
- 5
- 6 public class BubbleSort extends JApplet
- 7
- 8 // initialize applet
- 9 public void init()
- 10
- 11 JTextArea outputArea new
JTextArea() - 12 Container container
getContentPane() - 13 container.add( outputArea )
- 14
- 15 int array 2, 6, 4, 8, 10, 12,
89, 68, 45, 37 - 16
- 17 String output "Data items in
original order\n" - 18
- 19 // append original array values to
String output
Pass array by reference to method bubbleSort to
sort array
13- 35 // sort elements of array with bubble
sort - 36 public void bubbleSort( int array2 )
- 37
- 38 // loop to control number of passes
- 39 for ( int pass 1 pass lt
array2.length pass ) - 40
- 41 // loop to control number of
comparisons - 42 for ( int element 0
- 43 element lt array2.length - 1
- 44 element )
- 45
- 46 // compare side-by-side
elements and swap them if - 47 // first element is greater
than second element - 48 if ( array2 element gt
array2 element 1 ) - 49 swap( array2, element,
element 1 ) - 50
- 51 // end loop to control
comparisons - 52
- 53 // end loop to control passes
Method swap swaps two values in array reference
14Multidimensional Arrays
- Multidimensional arrays
- Tables with rows and columns
- Two-dimensional array
- Declaring two-dimensional array b22
- int b 1, 2 , 3, 4
- 1 and 2 initialize b00 and b01
- 3 and 4 initialize b10 and b11
- int b 1, 2 , 3, 4, 5
- row 0 contains elements 1 and 2
- row 1 contains elements 3, 4 and 5
15Multidimensional Arrays (Cont.)
- Creating multidimensional arrays
- Can be allocated dynamically
- 3-by-4 array
- int b b new int 3 4
- Rows can have different number of columns
- int b b new int 2 // allocate
rows b 0 new int 5 // allocate row 0
b 1 new int 3 // allocate row 1
16Column 0
Column 1
Column 2
Column 3
Row 0
a 0 0
a 0 1
a 0 2
a 0 3
Row 1
a 1 0
a 1 1
a 1 2
a 1 3
Row 2
a 2 0
a 2 1
a 2 2
a 2 3
Column index
Row index
Array name
Fig. 7.13 Two-dimensional array with three rows
and four columns.
17- 1 // Fig. 7.14 InitArray.java
- 2 // Initializing two-dimensional arrays.
- 3 import java.awt.Container
- 4 import javax.swing.
- 5
- 6 public class InitArray extends JApplet
- 7 JTextArea outputArea
- 8
- 9 // set up GUI and initialize applet
- 10 public void init()
- 11
- 12 outputArea new JTextArea()
- 13 Container container
getContentPane() - 14 container.add( outputArea )
- 15
- 16 int array1 1, 2, 3 , 4,
5, 6 - 17 int array2 1, 2 , 3 ,
4, 5, 6 - 18
- 19 outputArea.setText( "Values in array1
by row are\n" )
18- 27 // append rows and columns of an array
to outputArea - 28 public void buildOutput( int array )
- 29
- 30 // loop through array's rows
- 31 for ( int row 0 row lt
array.length row ) - 32
- 33 // loop through columns of current
row - 34 for ( int column 0 column lt
array row .length column ) - 35 outputArea.append( array row
column " " ) - 36
- 37 outputArea.append( "\n" )
- 38
- 39
- 40 // end method buildOutput
- 41
- 42 // end class InitArray
arrayrow.length returns number of columns
associated with row subscript
19Exercícios
- Escrever um programa que gera uma aposta
aleatória do totoloto - Nota o método Math.random() gera um número real
aleatório entre 0 (inclusivé) e 1 (exclusivé) - 2) Escrever um programa que gera N apostas
aleatórias do totoloto, todas diferentes entre
si, com N indicado pelo utilizador