Download presentation
Presentation is loading. Please wait.
Published byΣιλουανός Αλεξιάδης Modified over 6 years ago
1
Arrays An Array is an ordered collection of variables
One of our first “data structures” Gives some relationships between different data Usage: “store the grades of everybody in this class” Without arrays: one variable for each student With arrays: one variable for the whole class! It just stores one entry for each student
2
Arrays Arrays have a “Base Type”
E.g. “An array of ints” E.g. “An array of doubles” E.g. ”An array of Strings” Can be any built-in Java type, or a user-defined one (later) Each object inside the array is called an element
3
Arrays Declared by adding [ ] to the type: General syntax:
double[] testScores; // Only declares, does not create space testScores = new double[100]; // Allocates space in memeory // Now we can start using it! General syntax: ⟨array-variable ⟩ = new ⟨base-type ⟩[⟨array-length ⟩];
4
Arrays An array in memory is like a “list” of particular length int[] a = new int[5]; // Creates 5 elements in memory A A[0] A[1] A[2] A[3] A[4] The whole array is called A but individual elements are referenced with the [ ] operator The array has an attribute called “length” which returns the number of elements: A.length == 5 // will be true
5
Arrays To access individual elements of an array variable, use the [ ] operator: testScores[0] = 100.0; // Counting starts at 0 testScores[7] = 95.5; // Assigns 95 to the 8’th element We can use an int variable to index: int i = 10; testScores[i] = 80;
6
Default values Every variable gets set to a default when it is created
However, usually shouldn’t be relied on
7
Arrays and For-Loops We can use a for-loop to very easily traverse an array: int[] myArray = new int[100]; // Assigns each element random between 1 and 10 for( int i = 0; i < myArray.length; i++ ){ myArray[i] = (int) (10.0*Math.random() + 1); }
8
Arrays and For-loops Calculate the average of our random array:
double avg; double sum = 0; for( int = 0; i < myArray.length; i++ ){ sum = sum + myArray[i]; // OR sum += myArray[i]; } avg = sum / myArray.length;
9
Largest Array element Use linear search: check one-by-one, remember the biggest so far int biggest = myArray[0]; for( int i = 0; i<myArray.length; i++ ){ if ( myArray[i] > biggest ){ biggest = myArray[i]; } System.out.println(”The largest element is: “ + biggest);
10
Array Access Using a for-loop is an example of sequential access
Access elements one-by-one Another common way to access is by random access Can’t predict which will be used next Has hardware and memory implications
11
Example: Birthday paradox
How many people do you need to select at random until two have the same birthday? Internal bias: we only typically consider our own birthday, but there are hundreds of comparisons to be checked in a room of ~20 people! In pure statistics: with 23 randomly selected birthdays, there is a 50% chance that two of them are the same! Program goal: simulate drawing random birthdays until you get a match. Count the number of draws until a collision happens.
12
Multidimensional Arrays
Can be used to hold many “dimensions” of data E.g. Can think of an “array of arrays” Acts like a matrix or a table: int[][] myMatrix = new int[5][10]; // Declares 2-d, 5-by-10 array // with 50 int elements myMatrix[3][2] = 100; // Access like a matrix, each dimension at a time
13
Multidimensional Arrays
Example task: create a 2-d array to hold test scores of multiple students at once Print the average of each student Method: use a matrix where each student is a row and each column is an exam score
14
Example: Input Reverse
Algorithm Ask the user for up to 100 positive integers, enter a 0 to stop Store them each in an array Count how many the user entered Print the integers back to the user, in reverse order of how they were entered
15
Example: Finding the Mode
Algorithm: Generate an array of 100 random numbers from 1 to 10 Use a length 10 array to count how many times each number appears in the array Report the one that appears the most, or multiple if there are ties
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.