SlideShare uma empresa Scribd logo
1 de 22
Real Time Problem
Ramesh is a Teacher. He needs to store marks
of 12 students in his class.
HOW?
Solution 1
Ramesh can create twelve set of
variables to store marks of twelve
students.
S1 S2 S12
S3 … … ...
Mark1 Mark3 Mark12
Mark2
Solution 2
He can create one variable called mark in
that mark he can store marks of twelve
Students
S1 S2 S3 S3 S12
Mark
Array
Java array is an object which contains elements of a similar data type.
• 1D array
• 2D array
Array Types
How to declare an array
dataType[] arrayName;
Example:
int[] data;
data = new int[10];
(or)
int[] data=new int[10];
• Static 1D array
• Dynamic 1D array
Memory Allocation
Java array index
int data[]=new int[5];
Last element
First element
data[0] data[1] data[2] data[3] data[4]
Index Array data of length 5
1 2 3 4 5
0 1 2 3 4
• boolean : false
• int : 0
• double : 0.0
• String : null
• User Defined Type : null
Default array values
public class ArrayDemo
{
public static void main(String[] args){
String str[] = new String[5];
for (String s : str)
System.out.print(s + " ");
System.out.println();
int num[] = new int[5];
for (int val : num)
System.out.print(val + " ");
System.out.println();
double dnum[] = new double[5];
for (double val : dnum)
System.out.print(val + " ");
System.out.println();
boolean bnum[] = new boolean[5];
for (boolean val : bnum)
System.out.print(val + " ");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
null null null null null
0 0 0 0 0
0.0 0.0 0.0 0.0 0.0
false false false false false
OUTPUT
//Memory Allocation
int marks[] = new int[4];
marks[0] = 1
marks[1] = 2
marks[2] = 3
marks[3] = 4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1000
1001
1005
1006
1002
1003
1004
1007
1008
1009
...
2000
0
0
0
0
marks[0]
marks[1]
marks[2]
marks[3]
1
2
3
4
Storage of Arrays
Stack area
int marks[]= new int[]4
Heap Memory
1 2 3 4
How to access elements from an array?
First element - marks[0]
Second element – marks[1]
Third element – marks[2]
Fourth element – marks[3]
Fifth element – marks[4]
marks[ ] = { };
size = 5
5 15, 24, 45, 34, 60
To access any nth element,
marks[n-1]
0, 1, 2, 3, 4 - index
// Predict the output
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int[] arr = {12, 4, 5, 2, 5};
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Predict the output
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n = s.nextInt();
int arr[]=new int[n];
for(int i = 0; i < arr.length; i++)
{
arr[i] = s.nextInt();
}
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " " );
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Java Array of Objects
class Std {
String name;
public Std(String name){
this.name = name;
}
}
public class Main {
public static void main(String args[]) throws Exception {
Std myArray[] = new Std[3];
myArray [0] = new Std("Java");
myArray [1] = new Std("Object");
myArray [2] = new Std("Array");
for(Std str:myArray)
{
System.out.println(str.name);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Storage of Arrays
Stack area
myArray[]
Heap Memory
obj1 obj2 obj3
new Std(“Java”)
new
Std(“Object”)
new Std(“Array”)
Anonymous Array
• An array in Java without any name is called anonymous array.
• It is just for instant use (i.e. one time usage) .
• Anonymous array is passed as an argument of method.
Syntax:
new Datatype[] {val1, val2..valn};
import java.util.*;
public class MyClass {
public static void main(String args[])
{
int s1 = sum(new int[]{1,2,3,4,5});
System.out.print(s1);
}
public static int sum(int a[])
{
int total = 0;
for(int i:a)
{
total = total+i;
}
return total;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
THANK YOU

Mais conteúdo relacionado

Semelhante a WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-1D_Array.pptx

Semelhante a WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-1D_Array.pptx (20)

Unit 3
Unit 3 Unit 3
Unit 3
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
 
Session three Builders & developers workshop Microsoft Tech Club 2015
Session three Builders & developers workshop Microsoft Tech Club 2015Session three Builders & developers workshop Microsoft Tech Club 2015
Session three Builders & developers workshop Microsoft Tech Club 2015
 
arrays
arraysarrays
arrays
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
Building Java Programas
Building Java ProgramasBuilding Java Programas
Building Java Programas
 
Week7
Week7Week7
Week7
 
Array in C full basic explanation
Array in C full basic explanationArray in C full basic explanation
Array in C full basic explanation
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Array i imp
Array  i impArray  i imp
Array i imp
 
Array
ArrayArray
Array
 
Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Array
ArrayArray
Array
 
Java arrays
Java arraysJava arrays
Java arrays
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 

Mais de MaruMengesha

WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...MaruMengesha
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...MaruMengesha
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Mar-2021_L15...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Mar-2021_L15...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Mar-2021_L15...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Mar-2021_L15...MaruMengesha
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...MaruMengesha
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...MaruMengesha
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...MaruMengesha
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...MaruMengesha
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...MaruMengesha
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...MaruMengesha
 
Chemistry Student G9.pdf chemistry text book
Chemistry Student G9.pdf chemistry text bookChemistry Student G9.pdf chemistry text book
Chemistry Student G9.pdf chemistry text bookMaruMengesha
 
eco ppt.pptx Economics presentation Assignment
eco ppt.pptx Economics presentation Assignmenteco ppt.pptx Economics presentation Assignment
eco ppt.pptx Economics presentation AssignmentMaruMengesha
 
G12-Agriculture-STB-2023-web.pdf Agriculture text book
G12-Agriculture-STB-2023-web.pdf Agriculture text bookG12-Agriculture-STB-2023-web.pdf Agriculture text book
G12-Agriculture-STB-2023-web.pdf Agriculture text bookMaruMengesha
 

Mais de MaruMengesha (12)

WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_22-Feb-2021_L9-...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Mar-2021_L15...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Mar-2021_L15...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Mar-2021_L15...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Mar-2021_L15...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Feb-2021_L2_...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Feb-2021_L1_...
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_01-Mar-2021_L12...
 
Chemistry Student G9.pdf chemistry text book
Chemistry Student G9.pdf chemistry text bookChemistry Student G9.pdf chemistry text book
Chemistry Student G9.pdf chemistry text book
 
eco ppt.pptx Economics presentation Assignment
eco ppt.pptx Economics presentation Assignmenteco ppt.pptx Economics presentation Assignment
eco ppt.pptx Economics presentation Assignment
 
G12-Agriculture-STB-2023-web.pdf Agriculture text book
G12-Agriculture-STB-2023-web.pdf Agriculture text bookG12-Agriculture-STB-2023-web.pdf Agriculture text book
G12-Agriculture-STB-2023-web.pdf Agriculture text book
 

Último

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 

Último (20)

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 

WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_11-Feb-2021_L5-1D_Array.pptx

  • 1.
  • 2. Real Time Problem Ramesh is a Teacher. He needs to store marks of 12 students in his class. HOW?
  • 3. Solution 1 Ramesh can create twelve set of variables to store marks of twelve students. S1 S2 S12 S3 … … ... Mark1 Mark3 Mark12 Mark2
  • 4. Solution 2 He can create one variable called mark in that mark he can store marks of twelve Students S1 S2 S3 S3 S12 Mark
  • 5. Array Java array is an object which contains elements of a similar data type.
  • 6. • 1D array • 2D array Array Types
  • 7. How to declare an array dataType[] arrayName; Example: int[] data; data = new int[10]; (or) int[] data=new int[10];
  • 8. • Static 1D array • Dynamic 1D array Memory Allocation
  • 9. Java array index int data[]=new int[5]; Last element First element data[0] data[1] data[2] data[3] data[4] Index Array data of length 5 1 2 3 4 5 0 1 2 3 4
  • 10. • boolean : false • int : 0 • double : 0.0 • String : null • User Defined Type : null Default array values
  • 11. public class ArrayDemo { public static void main(String[] args){ String str[] = new String[5]; for (String s : str) System.out.print(s + " "); System.out.println(); int num[] = new int[5]; for (int val : num) System.out.print(val + " "); System.out.println(); double dnum[] = new double[5]; for (double val : dnum) System.out.print(val + " "); System.out.println(); boolean bnum[] = new boolean[5]; for (boolean val : bnum) System.out.print(val + " "); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 12. null null null null null 0 0 0 0 0 0.0 0.0 0.0 0.0 0.0 false false false false false OUTPUT
  • 13. //Memory Allocation int marks[] = new int[4]; marks[0] = 1 marks[1] = 2 marks[2] = 3 marks[3] = 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1000 1001 1005 1006 1002 1003 1004 1007 1008 1009 ... 2000 0 0 0 0 marks[0] marks[1] marks[2] marks[3] 1 2 3 4
  • 14. Storage of Arrays Stack area int marks[]= new int[]4 Heap Memory 1 2 3 4
  • 15. How to access elements from an array? First element - marks[0] Second element – marks[1] Third element – marks[2] Fourth element – marks[3] Fifth element – marks[4] marks[ ] = { }; size = 5 5 15, 24, 45, 34, 60 To access any nth element, marks[n-1] 0, 1, 2, 3, 4 - index
  • 16. // Predict the output import java.util.*; public class Main { public static void main(String[] args) { int[] arr = {12, 4, 5, 2, 5}; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 17. // Predict the output import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n = s.nextInt(); int arr[]=new int[n]; for(int i = 0; i < arr.length; i++) { arr[i] = s.nextInt(); } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " " ); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 18. //Java Array of Objects class Std { String name; public Std(String name){ this.name = name; } } public class Main { public static void main(String args[]) throws Exception { Std myArray[] = new Std[3]; myArray [0] = new Std("Java"); myArray [1] = new Std("Object"); myArray [2] = new Std("Array"); for(Std str:myArray) { System.out.println(str.name); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 19. Storage of Arrays Stack area myArray[] Heap Memory obj1 obj2 obj3 new Std(“Java”) new Std(“Object”) new Std(“Array”)
  • 20. Anonymous Array • An array in Java without any name is called anonymous array. • It is just for instant use (i.e. one time usage) . • Anonymous array is passed as an argument of method. Syntax: new Datatype[] {val1, val2..valn};
  • 21. import java.util.*; public class MyClass { public static void main(String args[]) { int s1 = sum(new int[]{1,2,3,4,5}); System.out.print(s1); } public static int sum(int a[]) { int total = 0; for(int i:a) { total = total+i; } return total; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Notas do Editor

  1. Description: Ramesh is creating twelve variable called mark1, mark2… mark12 to store marks of 12 students. It is more difficult if there 60 students in his class.
  2. Description: He can create a single variable name called mark. He can divide the mark into 12 columns so that he can store the marks of all 12 students in single record called mark. Here comes the array to fulfill our needs
  3. Description: Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
  4. 1D Array In this array which can 1 row and many columns i.e.) row = 1, column = n. Example: A deck of cards. 2D Array In this array which can multiple rows and columns i.e.) row = n, column = m. Example: Collection egg in an egg tray.
  5. Description: dataType can be a primitive data type like: int, char, Double, byte etc. or an object (will be discussed in later chapters). arrayName is an identifier. For example put a array name as our wish . Example: int name[]; char data[]; Double size[]; data =new int[10], 10 is an array length.
  6. Static Array Initilization: int arr[]={1,2,3,4,5}; (or) int arr[] = new int[5]; Dynamic Array Initilization: int arr[]=new int[n];
  7. Description: If we don’t assign values to array elements, and try to access them, compiler does not produce error as in case of simple variables. Instead it assigns values which aren’t garbage.
  8. Output
  9. Description: int is a primitive datatype. We know that the java program will load in stack memory. Since array is a reference. So the reference are stored in heap memory. Since it is a primitive datatype it is stored in adjacent memory location.
  10. Title + Text
  11. Note1: arr.length is used to return the number of elements in an array. In here, there are five elements in array “arr[]”. So it return’s value 5. Output: 12 4 5 2 5 Description: In this program array initialization is static. Because here we are initializing the array values before compiling the program. Here array length is 5 and i value start form 0. i<5 time loop has been excuted so output is 12 4 5 2 5. Note2: ArrayIndexOutOfBounds Exception It is a Runtime Exception thrown only at runtime. This exception occurs when we try to access the negative index or index greater than array size.
  12. Input: 5 1 2 3 4 5 Output: 1 2 3 4 5 Description: In this program array initilization is dynamic . i.e.) were creating the size of array during run time, so it is called Dynamic memory allocation. int ‘n’ is an array size and array elements are 1 2 3 4 5 so output is 1 2 3 4 5.
  13. Output: 15 Description: It is a program for anonymous array. I am creating an anonymous array. I want to find the sum of the array. While creating an anonymous array you should pass the that array to a method so that you can perform the desired function. Since there is no reference for anonymous array you can’t able to perform the operation in main method itself. On passing the anonymous array to a method. Now only temporary reference is created i.e.) int a[].Now the array values are fetched in array a[] ={1,2,3,4,5}. In this sum method the values in the array a[] are get summed and stored in total. This method returns the total i.e)15 to main method now the value is stored in s1.