SlideShare a Scribd company logo
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

PPT
ARRAYS.ppt
surajthakur474818
 
PPT
ARRAYSinJavaProgramming_Introduction.ppt
cluttertans
 
PPT
ARRAYS in java with in details presentation.ppt
AshokRachapalli1
 
PPT
ARRAYS.ppt
soniya555961
 
PPTX
6 arrays injava
irdginfo
 
PPTX
6_Array.pptx
shafat6712
 
DOCX
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPTX
Chap1 array
raksharao
 
ARRAYS.ppt
surajthakur474818
 
ARRAYSinJavaProgramming_Introduction.ppt
cluttertans
 
ARRAYS in java with in details presentation.ppt
AshokRachapalli1
 
ARRAYS.ppt
soniya555961
 
6 arrays injava
irdginfo
 
6_Array.pptx
shafat6712
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Chap1 array
raksharao
 

Similar to ARRAYS.ppt (20)

PPTX
Arrays in programming
TaseerRao
 
PPTX
Chapter6 (4) (1).pptx plog fix down more
mohammadalali41
 
PPT
ch06.ppt
ansariparveen06
 
PPT
ch06.ppt
chandrasekar529044
 
PPT
ch06.ppt
AqeelAbbas94
 
PPT
array Details
shivas379526
 
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
PPT
17-Arrays en java presentación documento
DiegoGamboaSafla
 
DOCX
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
PDF
Java - Arrays Concepts
Victer Paul
 
PDF
Array
Ravi_Kant_Sahu
 
PDF
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPT
Md05 arrays
Rakesh Madugula
 
PPT
JavaYDL6
Terry Yoast
 
PPT
Eo gaddis java_chapter_07_5e
Gina Bullock
 
PPT
Array
PRN USM
 
PDF
Learn Java Part 8
Gurpreet singh
 
PPTX
ch 7 single dimension array in oop .pptx
nilampatoliya
 
PPTX
javaArrays.pptx
AshishNayyar11
 
PPT
L10 array
teach4uin
 
Arrays in programming
TaseerRao
 
Chapter6 (4) (1).pptx plog fix down more
mohammadalali41
 
ch06.ppt
ansariparveen06
 
ch06.ppt
AqeelAbbas94
 
array Details
shivas379526
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
17-Arrays en java presentación documento
DiegoGamboaSafla
 
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Java - Arrays Concepts
Victer Paul
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Md05 arrays
Rakesh Madugula
 
JavaYDL6
Terry Yoast
 
Eo gaddis java_chapter_07_5e
Gina Bullock
 
Array
PRN USM
 
Learn Java Part 8
Gurpreet singh
 
ch 7 single dimension array in oop .pptx
nilampatoliya
 
javaArrays.pptx
AshishNayyar11
 
L10 array
teach4uin
 
Ad

Recently uploaded (20)

PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Basics and rules of probability with real-life uses
ravatkaran694
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Ad

ARRAYS.ppt

  • 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