SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Semelhante a ARRAYS.ppt (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
 

Mais de ssuser99ca78

Introduction-Chapter-1.ppt
Introduction-Chapter-1.pptIntroduction-Chapter-1.ppt
Introduction-Chapter-1.pptssuser99ca78
 
chapter2-(Intelligent Agents).ppt
chapter2-(Intelligent Agents).pptchapter2-(Intelligent Agents).ppt
chapter2-(Intelligent Agents).pptssuser99ca78
 
Java PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptxJava PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptxssuser99ca78
 
stringstringbuilderstringbuffer-190830060142.pptx
stringstringbuilderstringbuffer-190830060142.pptxstringstringbuilderstringbuffer-190830060142.pptx
stringstringbuilderstringbuffer-190830060142.pptxssuser99ca78
 
Java PSkills-session2.pptx
Java PSkills-session2.pptxJava PSkills-session2.pptx
Java PSkills-session2.pptxssuser99ca78
 
DSP_Course_Contents.ppt
DSP_Course_Contents.pptDSP_Course_Contents.ppt
DSP_Course_Contents.pptssuser99ca78
 
26 Speech Lecture.ppt
26 Speech Lecture.ppt26 Speech Lecture.ppt
26 Speech Lecture.pptssuser99ca78
 
Java PSkills Session-4 PNR.pptx
Java PSkills Session-4 PNR.pptxJava PSkills Session-4 PNR.pptx
Java PSkills Session-4 PNR.pptxssuser99ca78
 
Java PSkills Session-3 PNR.pptx
Java PSkills Session-3 PNR.pptxJava PSkills Session-3 PNR.pptx
Java PSkills Session-3 PNR.pptxssuser99ca78
 

Mais de ssuser99ca78 (13)

chapter3part1.ppt
chapter3part1.pptchapter3part1.ppt
chapter3part1.ppt
 
Introduction-Chapter-1.ppt
Introduction-Chapter-1.pptIntroduction-Chapter-1.ppt
Introduction-Chapter-1.ppt
 
chapter2-(Intelligent Agents).ppt
chapter2-(Intelligent Agents).pptchapter2-(Intelligent Agents).ppt
chapter2-(Intelligent Agents).ppt
 
2.ppt
2.ppt2.ppt
2.ppt
 
Java PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptxJava PSkills Session-6 PNR.pptx
Java PSkills Session-6 PNR.pptx
 
stringstringbuilderstringbuffer-190830060142.pptx
stringstringbuilderstringbuffer-190830060142.pptxstringstringbuilderstringbuffer-190830060142.pptx
stringstringbuilderstringbuffer-190830060142.pptx
 
Java PSkills-session2.pptx
Java PSkills-session2.pptxJava PSkills-session2.pptx
Java PSkills-session2.pptx
 
OOPJ.pptx
OOPJ.pptxOOPJ.pptx
OOPJ.pptx
 
DSP_Course_Contents.ppt
DSP_Course_Contents.pptDSP_Course_Contents.ppt
DSP_Course_Contents.ppt
 
26 Speech Lecture.ppt
26 Speech Lecture.ppt26 Speech Lecture.ppt
26 Speech Lecture.ppt
 
Java PSkills Session-4 PNR.pptx
Java PSkills Session-4 PNR.pptxJava PSkills Session-4 PNR.pptx
Java PSkills Session-4 PNR.pptx
 
Java PSkills Session-3 PNR.pptx
Java PSkills Session-3 PNR.pptxJava PSkills Session-3 PNR.pptx
Java PSkills Session-3 PNR.pptx
 
UNIT1-JAVA.pptx
UNIT1-JAVA.pptxUNIT1-JAVA.pptx
UNIT1-JAVA.pptx
 

Último

kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxNadaHaitham1
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEselvakumar948
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 

Último (20)

kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 

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