100 likes | 226 Views
http://discern.uits.iu.edu:8790/S517/S517.html. Web Programming. Array Xiaozhong Liu. An Array of… (Strings, integers, doubles). A list of Object. Information problem…. Output. Input. Process. An Object. A Variable (String, int , Double). Temperature File, 5 temperatures.
E N D
http://discern.uits.iu.edu:8790/S517/S517.html Web Programming Array Xiaozhong Liu
An Array of… (Strings, integers, doubles) A list of Object Information problem… Output Input Process An Object A Variable (String, int, Double)
Temperature File, 5 temperatures double [ ] temperatures = new double[5]; //Write to those variables (Array) 30.5 -15.3 0 60.35 18.12 temperatures [0] temperatures [1] temperatures [2] temperatures [3] temperatures [4]
Type Array name String Array String [ ] students = new String [25]; Array Declarations Variable value int Array int [ ] age = new int [100]; double double [ ] heights = new double [50];
int [ ] numbers = new int [50]; … //e.g.read from a file ‘Average numbers??? Average of numbers int sum = 0; double average; for (int index = 0; index<50; index++) { sum = sum + numbers [index]; } average = ????????
int [ ][ ] numbers = new int [2][3]; 2 – Dimensional Array
int [ ][ ] numbers = new int [3][5]; double average, sum = 0; 2 – Dimensional Array • for (int row= 0; row<3; row++) { • for (intcol = 0; col < 5; col++) { • sum = sum + numbers [row] [col]; • } • } • average = ?????????
int[ ] numbers = new int[20], squares = new int[20]; for (inti = 0; i < numbers.length; i++) { numbers[i] = i + 1; } //Compute squares??? //Average of squares??? Practice
double [ ] numbers = new double[5]; double [ ] numbers = {2.1, 43.1, 0, -0.3, 35.0}; You can use for loop How about dynamic array? Fix length array
double [ ] numbers = new double[5]; import java.util.ArrayList; ArrayListal = new ArrayList(); al.add("C"); al.add("A"); al.add("E"); al.add(1, "A2"); for (int i = 0; i < al.size(); i ++) { Stringitem = (String)al.get(i); System.out.println(item); } Dynamic array