Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Java arrays, Arrays

Array data structure


• An array is a data structure
• A data structure is an organized
collection of related data
• An array data types be one of primitive
(simple types)
• Array are data structures whose
elements are stored in
contiguous/adjacent areas in memory.
• They assessed using array name and an
index which determines value being
reference in memory.
Array data structure
• An array is a group of variables called
elements containing value that all of
have the same type
• The first element of a Java array has
index 0
• The last element of Java array has
index arraysize-1
• An array index is non negative number
with value one less than array’s size
Array data structure
array name

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;

Scanner input=new Scanner(System.in);


for(int i=0;i<=4;i++) { //inputing values in array
System.out.println("Enternumber"+(i+1)+":");
nums[i]= input.nextInt();
}
System.out.println("\n\tNUMBERS\tCUBE\n");
for(int i=0;i<=4;i++) { // cubing numbers
cb=nums[i]*nums[i]*nums[i];
System.out.println("\t"+i+"\t "+cb);
} } }
USING array INTIALIZER
You create an array and initialize
it using an array initializer
int[] numb={11, 12, 13, 14, 15, 16};

Creates a six element array


with indexes 0-5
Create array by array constructor
Your can use constructors of
primitive types or object types to
create an array.

int[] inum =new int[6];


double dnum=new double[4];
int[] inum=new int[4];
double[] dnum=new double[6];
for(int i=0;i<4;i++){
inum[i]=i*10;
}
// integers index multiplied by 10
System.out.println("index multiplied by 10");
for(int i=0;i<4;i++){
System.out.println(inum[i]);
}
for(int i=0;i<6;i++){
dnum[i]=Math.pow(8, i);
}
// power 6 raised to index
System.out.println("power 6 raised to index");
for(int i=0;i<6;i++){
System.out.println(dnum[i]);
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 languages start and
index value 0 or 1. In Java , C and C++
array index starts at 0. Java and C++
Supports multi-dimensional arrays.
Two dimension array

[0] [1] [2] k index [0] [1] [2]


[0] [0]
[1] [1]
I index [2] [2]
[3] [3] 8

int[[][] twodim= new int[4][3] int[3][1]=8


int[i][k]
Arrays class
Arrays is reference type and manipulates
arrays that contain several primitive values
of same data type.
Arrays class provides static methods for
manipulating arrays
Arrays Class is in java.util package. Its in
same package as Scanner.
Arrays.toString() used to convert arrays
String data type.
Arrays.sort(array) – sorts an array
Arrays.binarysearch(array, searchnumber)
– return location of searched number
Operators precedence
Precedence indicates order in which expression is
evaluated if it has several arithmetic operators
1. Multiplication, division and remainder applied first –
if expression has combination of this operators
they are applied left to right. Multiplication,
division and remainder have same level of
precedence
2. Addition and subtraction are evaluated next. If
expression contains both expression they are
evaluated left to right. Addition and subtraction
have same level of precedence
If some variables/values are in brackets- the sub
expression in brackets is evaluated first before all
other irrespective of arithmetic operator
Array class methods program
package arraymethods;
import java.util.Arrays;
public class ArrayMethods {
public static void main(String arg[])
{
try
{
int[] num= new int[3];
int fd;
num[0]=9;
num[1]=2;
num[2]=6;
//Arrays.fill(num,3);
Arrays.sort(num);
fd=Arrays.binarySearch(num,9);
System.out.println(fd);
for(int i=0;i<6;i++) {
System.out.println("num is = "+num[i]);
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("array out of bounds");
} } }
Exercise submit before beginning of
next class
Write a program that asks to enter marks one CAT out
of 50 and one assignment out of 50 and stores results
in arrays catm and assignm arrays respectively. It
adds the cat and assignment marks and displays the
results in reverse order.

Submit blackboard week 6 linkname catassARRAYwk6


before beginning of Monday class (9-10-2023)
Forming groups

Form groups of 3-4 students


submit group member names
blackboard week6 link
groupmembernames

Groups will be used to do group


exercises submit by 6-10-2023.

You might also like