The document provides an overview of arrays in Java, including one-dimensional and two-dimensional arrays, their declarations, and how to use initializer lists. It includes examples of code demonstrating the creation and manipulation of arrays, as well as common pitfalls and best practices. Additionally, it discusses the structure of arrays for storing various types of data, such as student names and grades.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
0 views20 pages
Week11 L1 Arrays
The document provides an overview of arrays in Java, including one-dimensional and two-dimensional arrays, their declarations, and how to use initializer lists. It includes examples of code demonstrating the creation and manipulation of arrays, as well as common pitfalls and best practices. Additionally, it discusses the structure of arrays for storing various types of data, such as student names and grades.
//******************************************************************** // Primes.java Author: Lewis/Loftus // // Demonstrates the use of an initializer list for an array. //********************************************************************
public class Primes
{ //----------------------------------------------------------------- // Stores some prime numbers in an array and prints them. //----------------------------------------------------------------- public static void main(String[] args) { int[] primeNums = {2, 3, 5, 7, 11, 13, 17, 19};
Output //******************************************************************** // Primes.java Author: Lewis/Loftus // Array length: 8 // Demonstrates The the first use of few an initializer list for prime numbers are:an array. //******************************************************************** 2 3 5 7 11 13 17 19 public class Primes { //----------------------------------------------------------------- // Stores some prime numbers in an array and prints them. //----------------------------------------------------------------- public static void main(String[] args) { int[] primeNums = {2, 3, 5, 7, 11, 13, 17, 19};
Tobe precise, in Java a two-dimensional array is an array of arrays A two-dimensional array is declared by specifying the size of each dimension separately: int[][] table = new int[12][50]; Anarray element is referenced using two index values: value = table[3][6] Thearray stored in one row can be specified using one index
//******************************************************************** // TwoDArray.java Author: Lewis/Loftus // // Demonstrates the use of a two-dimensional array. //********************************************************************
public class TwoDArray
{ //----------------------------------------------------------------- // Creates a 2D array of integers, fills it with increasing // integer values, then prints them out. //----------------------------------------------------------------- public static void main(String[] args) { 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;
Write an array declaration and any necessary supporting classes to represent the following statements: a. students’ names for a class of 25 students String[] students = new String[25]; b. students’ test grades for a class of 40 students int[] grades = new int[40]; or, for simple letter grades: char[] grades = new char[40]; or, for letter grades that include pluses and minuses String[] grades = new String[40]; c. students’ names for a class and homework grades for each student Student[] myClass = new Student[MAX]; public class Student { private String name; private int[] grades; }
Describe what problem occurs in the following code. What modifications shouldbe made to it to eliminate the problem? int[] numbers = {3, 2, 3, 6, 9, 10, 12, 32, 3, 12, 6}; for (int count = 1; count <= numbers.length; count++) System.out.println(numbers[count]); The for loop fails to print the 0th element of the array, and attempts to print the nonexistent 11th element of the array. As a consequence, an ArrayIndexOutOfBoundsException is thrown. The problem can be eliminated by providing a for loop which initializes count to 0 (rather than 1) and tests if count is less than (rather than less than or equal to) numbers.length.
Write code that sets each element of an array called nums to the value of the constant INITIAL. for (int index = 0; index < nums.length; index++) nums[index] = INITIAL; Write code that prints the values stored in an array called names backwards. for (int index = names.length-1; index >= 0; index--) System.out.println(names[index]); Write a method called sumArray that accepts an array of floating point values and returns the sum of the values stored in the array. public float sumArray(float[] values) { float sum = 0; for (int index = 0; index < values.length; index++) sum += values[index]; return sum;}