Professional Documents
Culture Documents
Handout 5 Arrays Arrays Conditioonal Operator Final
Handout 5 Arrays Arrays Conditioonal Operator Final
d[0] 16
d[1] 8
d[2] 11
d[3] 18 Array elements stored in contigous/adjacent areas
d[4] 40
d[5] 62
d[6] 84
d[7] 60
d[8] 6
array index
Array data structure
• You can use an array element in
calculation the same way you use variable
int sum, prod;
int[] marks ={50,60,40,90,95};
sum=marks[0]+marks[1];
System.out.println(sum);
/* outputs 110 first element plus second
element. */
prod=marks[3]/3;
System.out.println(prod);
/* outputs 30 third element divided by 3*/
Java data types
• Java data types divided into primitive
(simple) and reference types
• The data types we have been using: int,
short, long, float, double, boolean, byte,
char etc. have been primitive types.
• A primitive type variable can only hold
one value at a time. E.g. double variable
can only store one double value at a time
e.g. 9.4
• Reference types are used to store to
store location of objects
array data structure
• The arrays are defined at compile time
(static structures – not static keyword)
with size determined when program is
written.
• In java they store homogenous data of same
data type. Arrays can be single dimension or
multiple dimension.
• Multidimensional arrays can be two three or
more dimensions.
• Arrays in different programming languages
start with index value 0 or 1. In Java , C and
C++ array index starts at 0. Java and C++
Supports multi-dimensional arrays.
Cubing numbers array programs
package array;
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
int[] nums ={0,0,0,0,0};
int cb=0;