SlideShare a Scribd company logo
1 of 13
Arrays in JAVA
Visit for more Learning Resources
 Array is collection of related data items
 Creating an array
 Declare an array
 Create memory location
 Putting values to memory locations
Declaring an Array Variable
 Do not have to create an array while declaring array
variable
 <type> [ ] variable_name;
 Double[ ] myList;
 double myList[ ];
 Both syntaxes are equivalent
 No memory allocation at this point
Defining an Array
Define an array as follows:
 variable_name=new <type>[arraySize];
 Number = new int[5];
 Mylist = new int[10];
It creates an array using new dataType[arraySize];
 It assigns the reference of the newly created array to
the variable variable_name.
 dataType arrayname[ ] = {list of values};
 Int a [ ]={1,2,3,4,5,6,7,};
 Array index starts from 0 to arraySize-1;
 int is of 4 bytes, total space=4*10=40 bytes
Declaring and defining in the same statement:
Creating arrays cntd...
What happens if we define diffrent type…
 We define
 Int[ ] a=new long[20];
incompatible types
found: long[]
required: int[]
 The right hand side defines an array, and thus the array variable should
refer to the same type of array
Example:
int prime[100];
error=> ']' expected
long primes[20];
 The C++ style is not permitted in JAVA syntax
long[] primes = new long[20];
primes[25]=33;
Runtime Error:Exception in thread “main”
java.lang.ArrayIndexOutOfBoundsException
Array Size through Input
….
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
String inData;
int num;
System.out.println("Enter a Size for Array:");
inData = stdin.readLine();
num = Integer.parseInt( inData ); // convert inData to int
long[] primes = new long[num];
System.out.println(“Array Length=”+primes.length);
….
SAMPLE RUN:
Enter a Size for Array:
4
Array Length=4
Example for array
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList) {
System.out.println(element);
}
}
}
Otput:
1.9
2.9
3.4
3.5
Reusing Array Variables
 int[] primes=new int[10];
……
primes=new int[50];
 Previous array will be discarded
 Cannot alter the type of array
Demonstration
long[] primes = new long[20];
primes[0] = 2;
primes[1] = 3;
System.out.println(primes[0]);
System.out.println(primes[1]);
Output:
2
3
Array Length
 Refer to array length using length() method
 A data member of array object
 array_variable_name.length
 for(int k=0; k<primes.length;k++)
 Sample Code:
long[ ] primes = new long[20];
System.out.println(primes.length);
 Output: 20
 If number of elements in the array are changed,
JAVA will automatically change the length attribute!
Sample Program
class MinArray
{
public static void main ( String[] args )
{
int[] array = { 20, 19, 1, 5, 71, 27, 19, 95 } ;
int min=array[0]; // initialize the current minimum
for ( int index=0; index < array.length; index++ )
if ( array[ index ] < min )
min = array[ index ] ;
System.out.println("The minimum of this array is: " + min );
}
}
*Program taken from: http://chortle.ccsu.edu/CS151/Notes/chap47/ch47_10.html
Two dimenssional array
 Representing 2D arrays
 Int myarray[][];
 Myarray = new int[3][4];
 Int myarray [][] = new int[3][4];
 Example
 Int myarray[2][3]={0,0,0,1,1,1};
2 columns and 3 rows
For more detail contact us

More Related Content

Similar to Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

Similar to Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. (20)

Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Learn Java Part 9
Learn Java Part 9Learn Java Part 9
Learn Java Part 9
 
7.basic array
7.basic array7.basic array
7.basic array
 
Lecture 6 - Arrays
Lecture 6 - ArraysLecture 6 - Arrays
Lecture 6 - Arrays
 
Lecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdfLecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdf
 
Array
ArrayArray
Array
 
Array lecture
Array lectureArray lecture
Array lecture
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
Arrays
ArraysArrays
Arrays
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Array
ArrayArray
Array
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 

More from ssuser6478a8

HTML is a markup language used by the browser to manipulate text, images, and...
HTML is a markup language used by the browser to manipulate text, images, and...HTML is a markup language used by the browser to manipulate text, images, and...
HTML is a markup language used by the browser to manipulate text, images, and...ssuser6478a8
 
Buila a Personalized Online course Recommender System with Machine Learning
Buila a Personalized Online course Recommender System with Machine LearningBuila a Personalized Online course Recommender System with Machine Learning
Buila a Personalized Online course Recommender System with Machine Learningssuser6478a8
 
5G is the fifth-generation technology standard for cellular networks, which c...
5G is the fifth-generation technology standard for cellular networks, which c...5G is the fifth-generation technology standard for cellular networks, which c...
5G is the fifth-generation technology standard for cellular networks, which c...ssuser6478a8
 
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...ssuser6478a8
 
HTML element is everything between the start tag and the end tag
HTML element is everything between the start tag and the end tagHTML element is everything between the start tag and the end tag
HTML element is everything between the start tag and the end tagssuser6478a8
 
Data mining is the statistical technique of processing raw data in a structur...
Data mining is the statistical technique of processing raw data in a structur...Data mining is the statistical technique of processing raw data in a structur...
Data mining is the statistical technique of processing raw data in a structur...ssuser6478a8
 
HTML stands for HyperText Markup Language. It is used to design web pages usi...
HTML stands for HyperText Markup Language. It is used to design web pages usi...HTML stands for HyperText Markup Language. It is used to design web pages usi...
HTML stands for HyperText Markup Language. It is used to design web pages usi...ssuser6478a8
 
Network Security and its applications in
Network Security and its applications inNetwork Security and its applications in
Network Security and its applications inssuser6478a8
 
Arrays and with its types and elements in java
Arrays and with its types and elements in javaArrays and with its types and elements in java
Arrays and with its types and elements in javassuser6478a8
 

More from ssuser6478a8 (9)

HTML is a markup language used by the browser to manipulate text, images, and...
HTML is a markup language used by the browser to manipulate text, images, and...HTML is a markup language used by the browser to manipulate text, images, and...
HTML is a markup language used by the browser to manipulate text, images, and...
 
Buila a Personalized Online course Recommender System with Machine Learning
Buila a Personalized Online course Recommender System with Machine LearningBuila a Personalized Online course Recommender System with Machine Learning
Buila a Personalized Online course Recommender System with Machine Learning
 
5G is the fifth-generation technology standard for cellular networks, which c...
5G is the fifth-generation technology standard for cellular networks, which c...5G is the fifth-generation technology standard for cellular networks, which c...
5G is the fifth-generation technology standard for cellular networks, which c...
 
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
Data Structures and Algorithms (DSA) is a fundamental part of Computer Scienc...
 
HTML element is everything between the start tag and the end tag
HTML element is everything between the start tag and the end tagHTML element is everything between the start tag and the end tag
HTML element is everything between the start tag and the end tag
 
Data mining is the statistical technique of processing raw data in a structur...
Data mining is the statistical technique of processing raw data in a structur...Data mining is the statistical technique of processing raw data in a structur...
Data mining is the statistical technique of processing raw data in a structur...
 
HTML stands for HyperText Markup Language. It is used to design web pages usi...
HTML stands for HyperText Markup Language. It is used to design web pages usi...HTML stands for HyperText Markup Language. It is used to design web pages usi...
HTML stands for HyperText Markup Language. It is used to design web pages usi...
 
Network Security and its applications in
Network Security and its applications inNetwork Security and its applications in
Network Security and its applications in
 
Arrays and with its types and elements in java
Arrays and with its types and elements in javaArrays and with its types and elements in java
Arrays and with its types and elements in java
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

  • 1. Arrays in JAVA Visit for more Learning Resources
  • 2.  Array is collection of related data items  Creating an array  Declare an array  Create memory location  Putting values to memory locations
  • 3. Declaring an Array Variable  Do not have to create an array while declaring array variable  <type> [ ] variable_name;  Double[ ] myList;  double myList[ ];  Both syntaxes are equivalent  No memory allocation at this point
  • 4. Defining an Array Define an array as follows:  variable_name=new <type>[arraySize];  Number = new int[5];  Mylist = new int[10]; It creates an array using new dataType[arraySize];  It assigns the reference of the newly created array to the variable variable_name.  dataType arrayname[ ] = {list of values};  Int a [ ]={1,2,3,4,5,6,7,};  Array index starts from 0 to arraySize-1;  int is of 4 bytes, total space=4*10=40 bytes Declaring and defining in the same statement:
  • 6. What happens if we define diffrent type…  We define  Int[ ] a=new long[20]; incompatible types found: long[] required: int[]  The right hand side defines an array, and thus the array variable should refer to the same type of array Example: int prime[100]; error=> ']' expected long primes[20];  The C++ style is not permitted in JAVA syntax long[] primes = new long[20]; primes[25]=33; Runtime Error:Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException
  • 7. Array Size through Input …. BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); String inData; int num; System.out.println("Enter a Size for Array:"); inData = stdin.readLine(); num = Integer.parseInt( inData ); // convert inData to int long[] primes = new long[num]; System.out.println(“Array Length=”+primes.length); …. SAMPLE RUN: Enter a Size for Array: 4 Array Length=4
  • 8. Example for array public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (double element: myList) { System.out.println(element); } } } Otput: 1.9 2.9 3.4 3.5
  • 9. Reusing Array Variables  int[] primes=new int[10]; …… primes=new int[50];  Previous array will be discarded  Cannot alter the type of array
  • 10. Demonstration long[] primes = new long[20]; primes[0] = 2; primes[1] = 3; System.out.println(primes[0]); System.out.println(primes[1]); Output: 2 3
  • 11. Array Length  Refer to array length using length() method  A data member of array object  array_variable_name.length  for(int k=0; k<primes.length;k++)  Sample Code: long[ ] primes = new long[20]; System.out.println(primes.length);  Output: 20  If number of elements in the array are changed, JAVA will automatically change the length attribute!
  • 12. Sample Program class MinArray { public static void main ( String[] args ) { int[] array = { 20, 19, 1, 5, 71, 27, 19, 95 } ; int min=array[0]; // initialize the current minimum for ( int index=0; index < array.length; index++ ) if ( array[ index ] < min ) min = array[ index ] ; System.out.println("The minimum of this array is: " + min ); } } *Program taken from: http://chortle.ccsu.edu/CS151/Notes/chap47/ch47_10.html
  • 13. Two dimenssional array  Representing 2D arrays  Int myarray[][];  Myarray = new int[3][4];  Int myarray [][] = new int[3][4];  Example  Int myarray[2][3]={0,0,0,1,1,1}; 2 columns and 3 rows For more detail contact us