Java - Arrays
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Introduction
 An array is a data structure that consists of an ordered
collection of similar items.
 An array has a single name.
 The items in an array are referred / accessed in terms of
their position / index in the array.
 The items in an array are called elements.
 All of the elements need to be of the same type.
 The type can be any primitive or reference type.
 The length of an array is measured by the number of
elements.
 The first element is element[0], the second is
element[1], etc.
 An item’s position with an array is its index or subscript.
2Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Introduction
3Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Introduction
 Declaring Array Variables (One Dimensional Arrays)
 Creating Arrays
 Declaring and Creating in One Step
 Array Initialization
 Default Values
 The Length of an Array
 Array Size through Input
 Accessing Array Elements
 Validating Indexes
 Two Dimensional Arrays
4Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Declaring Array Variables
 Syntax: datatype[] arrayRefVar;
 Example: double[] myList;
 Alternative syntax: datatype arrayRefVar[];
// This style is allowed, but not preferred
 Example:
 double myDouble[];
 Int[] myInt;
5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Creating Arrays
 Syntax:
arrayRefVar = new datatype[arraySize];
 Example:
myDouble = new double[10];
 myDouble[0] references the first element in the array.
 myDouble[9] references the last element in the array.
myInt = new int[5];
6Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Declaring and Creating in One Step
 Syntax:
datatype[] arrayRefVar = new datatype[arraySize];
 Example: double[] myDouble = new double[10];
 Alternative Syntax:
datatype arrayRefVar[] = new datatype[arraySize];
 Example: int myInt[] = new int[10];
String[] myString = new String[5];
MyClass[] myObject= new MyClass[2]; //object
7Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Array Initializers
 Java has a shorthand notation to declare, create, and
initialize an array in one statement:
double[] myList = {1.9, 2.9, 3.4, 3.5};
 This shorthand notation is equivalent to the following
statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
CAUTION
 This shorthand syntax must be in one statement
 The following is incorrect:
double[] myList = new double[4];
myList= {1.9, 2.9, 3.4, 3.5};
9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Default Initialization
 When array is created, array elements are initialized
 Numeric values (int, double, etc.) to 0
 Boolean values to false
 Char values to ‘u0000’ (unicode for blank character)
 Class types to null
10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The Length of an Array
 Once an array is created, its size is fixed
 Array size cannot be changed
 We can find its size using the following syntax:
arrayRefVar.length
 Note that length is a constant, not a method
 There is no parentheses following length
 For example, myList.length returns 10
 myInt.length
Sample Code:
long[] primes = new long[20];
System.out.println(primes.length);
Output: 20
11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Accessing Array Elements
 Index of an array is defined as
 Positive int, byte or short values
 Expression that results into these types
 Any other types used for index will give error
 long, double, etc.
 Incase Expression results in long, then type cast to int
 Indexing starts from 0 and ends at N-1
primes[2]=0;
int k = primes[2];
…
12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Array Size through Input
13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
What happens if …
long[] primes = new long[20];
primes[25]=33;
….
Runtime Error:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 25
at MorePrimes.main(MorePrimes.java:6)
14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
15
Copying Arrays (The Wrong Way)
 Often, in a program, you need to duplicate an array or a part of an array.
In such cases you could attempt to use the assignment statement (=), as
follows:
myList1= new int[10];
myList2 = new int[10];
myList2 = myList1;
Contents
of myList1
list1
Contents
of myList2
list2
Before the assignment
Contents
of myList1
list1
Contents
of myList2
list2
After the assignment
Garbage
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
16
Copying Arrays (The Correct Way)
 Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
for (int i = 0; i < sourceArrays.length; i++)
targetArray[i] = sourceArray[i];
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
17
Passing Arrays to Methods
 Defining a method that takes an int array as a formal
parameter
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
 Invoking the method with an int array as an argument
(actual parameter)
int[] list = {3, 1, 2, 6, 4, 2};
printArray(list);
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
18
Returning an Array from a Method
 Define the method that returns an array
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
for (int i = 0, j = result.length - 1;
i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}
 Call the method
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Arrays of Arrays
 Two-Dimensional arrays
 float[][] temperature=new float[10][365];
 10 arrays each having 365 elements
 First index: specifies array (row)
 Second Index: specifies element in that array (column)
 In JAVA float is 4 bytes, total Size=4*10*365=14,600 bytes
19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Graphical Representation
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Sample[0]
Sample[1]
Sample[2]
20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Initializing Array of Arrays
int[][] array2D = {
{99, 42, 74, 83, 100},
{90, 91, 72, 88, 95},
{88, 61, 74, 89, 96},
{61, 89, 82, 98, 93},
{93, 73, 75, 78, 99},
};
//5 arrays with 5 elements each
21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Sample Program
class unevenExample3
{
public static void main( String[] arg )
{ // declare and construct a 2D array
int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } };
// print out the array
for ( int row=0; row < uneven.length; row++ ) //changes row
{
System.out.print("Row " + row + ": ");
for ( int col=0; col < uneven[row].length; col++ ) //changes column
System.out.print( uneven[row][col] + " ");
System.out.println();
}
}
}
22Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Output
Row 0: 1 9 4
Row 1: 0 2
Row 2: 0 1 2 3 4
23Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Multidimensional Arrays
 A farmer has 10 farms of beans each in 5 countries, and
each farm has 30 fields!
 Three-dimensional array
long[][][] beans=new long[5][10][30];
//beans[country][farm][fields]
24Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
25
Examples of Arrays Processing
 Initializing an array with random values between 0.0
and 99.0
double[] myList = new double[4];
for (int i = 0; i < myList.length; i++) {
myList[i] = Math.random() * 100;
}
 Printing an array
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i] + " ");
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
26
 Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
 Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max)
max = myList[i];
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
27
 Finding the smallest index of the largest element
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
28
The End…
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

More Related Content

PDF
Java - Class Structure
PPTX
DSA 103 Object Oriented Programming :: Week 5
PDF
Hibernate Query Language
PDF
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
PPTX
Control statements
PPT
Chapter 11 ds
PPT
PPTX
DSA 103 Object Oriented Programming :: Week 4
Java - Class Structure
DSA 103 Object Oriented Programming :: Week 5
Hibernate Query Language
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
Control statements
Chapter 11 ds
DSA 103 Object Oriented Programming :: Week 4

What's hot (19)

PDF
DSA 103 Object Oriented Programming :: Week 3
PPTX
Icom4015 lecture14-f16
PPT
Java™ (OOP) - Chapter 6: "Arrays"
PPTX
Icom4015 lecture13-f16
PPTX
Icom4015 lecture15-f16
ODP
James Jesus Bermas on Crash Course on Python
PPTX
6 arrays injava
PPT
Hub102 - JS - Lesson3
PPTX
arrays-120712074248-phpapp01
PPT
PPTX
Icom4015 lecture4-f17
PPTX
Advanced Programming Lecture 6 Fall 2016
PPTX
Icom4015 lecture7-f16
PPT
Data structures
PPTX
Computer programming 2 Lesson 13
PPTX
18. Dictionaries, Hash-Tables and Set
PDF
Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...
PPTX
Icom4015 lecture4-f16
PPT
Chapter 10: hashing data structure
DSA 103 Object Oriented Programming :: Week 3
Icom4015 lecture14-f16
Java™ (OOP) - Chapter 6: "Arrays"
Icom4015 lecture13-f16
Icom4015 lecture15-f16
James Jesus Bermas on Crash Course on Python
6 arrays injava
Hub102 - JS - Lesson3
arrays-120712074248-phpapp01
Icom4015 lecture4-f17
Advanced Programming Lecture 6 Fall 2016
Icom4015 lecture7-f16
Data structures
Computer programming 2 Lesson 13
18. Dictionaries, Hash-Tables and Set
Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...
Icom4015 lecture4-f16
Chapter 10: hashing data structure
Ad

Similar to Java - Arrays Concepts (20)

PPTX
Chapter 7.1
PPTX
Arrays in programming
PPT
Array
PPTX
Arrays and Strings engineering education
PDF
An Introduction to Programming in Java: Arrays
PDF
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PDF
Week06
PPTX
6_Array.pptx
PPT
Eo gaddis java_chapter_07_5e
PPTX
07+08slide.pptx
PDF
PPTX
javaArrays.pptx
PPTX
Java arrays
PDF
Lecture 6 - Arrays
PPT
ch07-arrays.ppt
PPT
ARRAYS.ppt
PPT
ARRAYS.ppt
PPT
Array and 2D Array and with syntax working
PPT
Arrays are used to store multiple values in a single variable, instead of dec...
Chapter 7.1
Arrays in programming
Array
Arrays and Strings engineering education
An Introduction to Programming in Java: Arrays
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Week06
6_Array.pptx
Eo gaddis java_chapter_07_5e
07+08slide.pptx
javaArrays.pptx
Java arrays
Lecture 6 - Arrays
ch07-arrays.ppt
ARRAYS.ppt
ARRAYS.ppt
Array and 2D Array and with syntax working
Arrays are used to store multiple values in a single variable, instead of dec...
Ad

More from Victer Paul (12)

PDF
OOAD - UML - Sequence and Communication Diagrams - Lab
PDF
OOAD - UML - Class and Object Diagrams - Lab
PDF
OOAD - Systems and Object Orientation Concepts
PDF
Java - Strings Concepts
PDF
Java - Packages Concepts
PDF
Java - OOPS and Java Basics
PDF
Java - Exception Handling Concepts
PDF
Java - Object Oriented Programming Concepts
PDF
Java - Basic Concepts
PDF
Java - File Input Output Concepts
PDF
Java - Inheritance Concepts
PDF
Java applet programming concepts
OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
OOAD - Systems and Object Orientation Concepts
Java - Strings Concepts
Java - Packages Concepts
Java - OOPS and Java Basics
Java - Exception Handling Concepts
Java - Object Oriented Programming Concepts
Java - Basic Concepts
Java - File Input Output Concepts
Java - Inheritance Concepts
Java applet programming concepts

Recently uploaded (20)

PDF
Advancements in abstractive text summarization: a deep learning approach
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
PDF
Addressing the challenges of harmonizing law and artificial intelligence tech...
PDF
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
PPTX
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
PDF
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
PPTX
CRM(Customer Relationship Managmnet) Presentation
PDF
Revolutionizing recommendations a survey: a comprehensive exploration of mode...
PDF
FASHION-DRIVEN TEXTILES AS A CRYSTAL OF A NEW STREAM FOR STAKEHOLDER CAPITALI...
PPTX
Introduction-to-Artificial-Intelligence (1).pptx
PPTX
Report in SIP_Distance_Learning_Technology_Impact.pptx
PDF
Introduction to c language from lecture slides
PDF
The Digital Engine Room: Unlocking APAC’s Economic and Digital Potential thro...
PDF
Be ready for tomorrow’s needs with a longer-lasting, higher-performing PC
PDF
Uncertainty-aware contextual multi-armed bandits for recommendations in e-com...
PPTX
From Curiosity to ROI — Cost-Benefit Analysis of Agentic Automation [3/6]
PDF
Ebook - The Future of AI A Comprehensive Guide.pdf
PDF
Peak of Data & AI Encore: Scalable Design & Infrastructure
PDF
TicketRoot: Event Tech Solutions Deck 2025
Advancements in abstractive text summarization: a deep learning approach
Presentation - Principles of Instructional Design.pptx
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
Addressing the challenges of harmonizing law and artificial intelligence tech...
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
CRM(Customer Relationship Managmnet) Presentation
Revolutionizing recommendations a survey: a comprehensive exploration of mode...
FASHION-DRIVEN TEXTILES AS A CRYSTAL OF A NEW STREAM FOR STAKEHOLDER CAPITALI...
Introduction-to-Artificial-Intelligence (1).pptx
Report in SIP_Distance_Learning_Technology_Impact.pptx
Introduction to c language from lecture slides
The Digital Engine Room: Unlocking APAC’s Economic and Digital Potential thro...
Be ready for tomorrow’s needs with a longer-lasting, higher-performing PC
Uncertainty-aware contextual multi-armed bandits for recommendations in e-com...
From Curiosity to ROI — Cost-Benefit Analysis of Agentic Automation [3/6]
Ebook - The Future of AI A Comprehensive Guide.pdf
Peak of Data & AI Encore: Scalable Design & Infrastructure
TicketRoot: Event Tech Solutions Deck 2025

Java - Arrays Concepts

  • 1. Java - Arrays Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 2. Introduction  An array is a data structure that consists of an ordered collection of similar items.  An array has a single name.  The items in an array are referred / accessed in terms of their position / index in the array.  The items in an array are called elements.  All of the elements need to be of the same type.  The type can be any primitive or reference type.  The length of an array is measured by the number of elements.  The first element is element[0], the second is element[1], etc.  An item’s position with an array is its index or subscript. 2Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 3. Introduction 3Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 4. Introduction  Declaring Array Variables (One Dimensional Arrays)  Creating Arrays  Declaring and Creating in One Step  Array Initialization  Default Values  The Length of an Array  Array Size through Input  Accessing Array Elements  Validating Indexes  Two Dimensional Arrays 4Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 5. Declaring Array Variables  Syntax: datatype[] arrayRefVar;  Example: double[] myList;  Alternative syntax: datatype arrayRefVar[]; // This style is allowed, but not preferred  Example:  double myDouble[];  Int[] myInt; 5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 6. Creating Arrays  Syntax: arrayRefVar = new datatype[arraySize];  Example: myDouble = new double[10];  myDouble[0] references the first element in the array.  myDouble[9] references the last element in the array. myInt = new int[5]; 6Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 7. Declaring and Creating in One Step  Syntax: datatype[] arrayRefVar = new datatype[arraySize];  Example: double[] myDouble = new double[10];  Alternative Syntax: datatype arrayRefVar[] = new datatype[arraySize];  Example: int myInt[] = new int[10]; String[] myString = new String[5]; MyClass[] myObject= new MyClass[2]; //object 7Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 8. Array Initializers  Java has a shorthand notation to declare, create, and initialize an array in one statement: double[] myList = {1.9, 2.9, 3.4, 3.5};  This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; 8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 9. CAUTION  This shorthand syntax must be in one statement  The following is incorrect: double[] myList = new double[4]; myList= {1.9, 2.9, 3.4, 3.5}; 9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 10. Default Initialization  When array is created, array elements are initialized  Numeric values (int, double, etc.) to 0  Boolean values to false  Char values to ‘u0000’ (unicode for blank character)  Class types to null 10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 11. The Length of an Array  Once an array is created, its size is fixed  Array size cannot be changed  We can find its size using the following syntax: arrayRefVar.length  Note that length is a constant, not a method  There is no parentheses following length  For example, myList.length returns 10  myInt.length Sample Code: long[] primes = new long[20]; System.out.println(primes.length); Output: 20 11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 12. Accessing Array Elements  Index of an array is defined as  Positive int, byte or short values  Expression that results into these types  Any other types used for index will give error  long, double, etc.  Incase Expression results in long, then type cast to int  Indexing starts from 0 and ends at N-1 primes[2]=0; int k = primes[2]; … 12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 13. Array Size through Input 13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 14. What happens if … long[] primes = new long[20]; primes[25]=33; …. Runtime Error: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 25 at MorePrimes.main(MorePrimes.java:6) 14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 15. 15 Copying Arrays (The Wrong Way)  Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows: myList1= new int[10]; myList2 = new int[10]; myList2 = myList1; Contents of myList1 list1 Contents of myList2 list2 Before the assignment Contents of myList1 list1 Contents of myList2 list2 After the assignment Garbage Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 16. 16 Copying Arrays (The Correct Way)  Using a loop: int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i]; Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 17. 17 Passing Arrays to Methods  Defining a method that takes an int array as a formal parameter public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } }  Invoking the method with an int array as an argument (actual parameter) int[] list = {3, 1, 2, 6, 4, 2}; printArray(list); Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 18. 18 Returning an Array from a Method  Define the method that returns an array public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; }  Call the method int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 19. Arrays of Arrays  Two-Dimensional arrays  float[][] temperature=new float[10][365];  10 arrays each having 365 elements  First index: specifies array (row)  Second Index: specifies element in that array (column)  In JAVA float is 4 bytes, total Size=4*10*365=14,600 bytes 19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 20. Graphical Representation 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 Sample[0] Sample[1] Sample[2] 20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 21. Initializing Array of Arrays int[][] array2D = { {99, 42, 74, 83, 100}, {90, 91, 72, 88, 95}, {88, 61, 74, 89, 96}, {61, 89, 82, 98, 93}, {93, 73, 75, 78, 99}, }; //5 arrays with 5 elements each 21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 22. Sample Program class unevenExample3 { public static void main( String[] arg ) { // declare and construct a 2D array int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } }; // print out the array for ( int row=0; row < uneven.length; row++ ) //changes row { System.out.print("Row " + row + ": "); for ( int col=0; col < uneven[row].length; col++ ) //changes column System.out.print( uneven[row][col] + " "); System.out.println(); } } } 22Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 23. Output Row 0: 1 9 4 Row 1: 0 2 Row 2: 0 1 2 3 4 23Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 24. Multidimensional Arrays  A farmer has 10 farms of beans each in 5 countries, and each farm has 30 fields!  Three-dimensional array long[][][] beans=new long[5][10][30]; //beans[country][farm][fields] 24Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 25. 25 Examples of Arrays Processing  Initializing an array with random values between 0.0 and 99.0 double[] myList = new double[4]; for (int i = 0; i < myList.length; i++) { myList[i] = Math.random() * 100; }  Printing an array for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " "); } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 26. 26  Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; }  Finding the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 27. 27  Finding the smallest index of the largest element double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 28. 28 The End… Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam