SlideShare uma empresa Scribd logo
1 de 13
ARRAYS
An array refers to a data structure that contains
homogeneous elements. This means that all the
elements in the array are of the same data type.
ARRAYS
There are three main features of an array:
Dynamic allocation:
In arrays, the memory is created dynamically,
which reduces the amount of storage required for the
code.
Elements stored under a single name:
All the elements are stored under one name.
This name is used any time we use an array.
Occupies contiguous location:
The elements in the arrays are stored at
adjacent positions. This makes it easy for the user to
find the locations of its elements.
Advantages of Arrays in Java
• Java arrays enable you to access any element
randomly with the help of indexes
• It is easy to store and manipulate large data sets
Disadvantages of Arrays in Java
• The size of the array cannot be increased or
decreased once it is declared—arrays have a fixed
size
• Java cannot store heterogeneous data. It can only
store a single type of primitives
• Now that we understand what Java arrays are- let
us look at how arrays in Java are declared and
defined.
Define an Array in Java
Arrays in Java are easy to define and declare.
First, we have to define the array. The syntax
for it is:
Here, the type is int, String, double, or long.
Var-name is the variable name of the array.
byte a[];
short b[];
boolean booleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];
Instantiating an Array in Java
When an array is declared, only a reference of
an array is created. To create or give memory
to the array, you create an array like this:
The general form of new as it applies to one-
dimensional arrays appears as follows:
type var-name[];//array declaration
var-name = new type [size];
Example:
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to
array
OR
int[] intArray = new int[20]; // combining both
statements in one
Array Literal
The elements in the array allocated by new will
automatically be initialized to zero (for numeric
types), false (for boolean), or null (for reference
types).
In a situation where the size of the array and
variables of the array are already known, array
literals can be used.
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
// Declaring array literalThe length of this array
determines the length of the created array.
Types of Arrays:
There are three types of arrays.
We use these types of arrays as per the
requirement of the program.
1.One-dimensional Array
Also known as a linear array, the elements are
stored in a single row.
For example:
2. Two-dimensional Array
Two-dimensional arrays store the data in rows
and columns:
class GFG {
public static void main(String[] args)
{
// declares an Array of integers.
int[] arr;
// allocating memory for 5 integers.
arr = new int[5];
// initialize the first elements of the array
arr[0] = 10;
// initialize the second elements of the array
arr[1] = 20;
// so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i
+ " : " + arr[i]);
}
}
3. Multi-dimensional Array
This is a combination of two or more arrays
or nested arrays.
We can even use more than two rows and
columns using the following code:
//Java Program to illustrate the use of multidimensional array
class Testarray3
{
public static void main(String args[])
{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}

Mais conteúdo relacionado

Semelhante a ARRAYS.pptx

Semelhante a ARRAYS.pptx (20)

Arrays declartion and initialization
Arrays declartion and initializationArrays declartion and initialization
Arrays declartion and initialization
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
What-is-an-Array-in-Java-and-How-to-Create-One.pptx
What-is-an-Array-in-Java-and-How-to-Create-One.pptxWhat-is-an-Array-in-Java-and-How-to-Create-One.pptx
What-is-an-Array-in-Java-and-How-to-Create-One.pptx
 
Arrays accessing using for loops
Arrays accessing using for loopsArrays accessing using for loops
Arrays accessing using for loops
 
OOPs with java
OOPs with javaOOPs with java
OOPs with java
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
 
Ppt chapter09
Ppt chapter09Ppt chapter09
Ppt chapter09
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
 
Types of Arrays
Types of ArraysTypes of Arrays
Types of Arrays
 
Chapter 6 java
Chapter 6 javaChapter 6 java
Chapter 6 java
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
Unit 2
Unit 2Unit 2
Unit 2
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Lesson 11 one dimensional array
Lesson 11 one dimensional arrayLesson 11 one dimensional array
Lesson 11 one dimensional array
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
 

Último

Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal 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
 
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
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
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
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
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
 
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
 
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
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 

Último (20)

Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
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...
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
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
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
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
 
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
 
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
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 

ARRAYS.pptx

  • 1. ARRAYS An array refers to a data structure that contains homogeneous elements. This means that all the elements in the array are of the same data type.
  • 2. ARRAYS There are three main features of an array: Dynamic allocation: In arrays, the memory is created dynamically, which reduces the amount of storage required for the code. Elements stored under a single name: All the elements are stored under one name. This name is used any time we use an array. Occupies contiguous location: The elements in the arrays are stored at adjacent positions. This makes it easy for the user to find the locations of its elements.
  • 3. Advantages of Arrays in Java • Java arrays enable you to access any element randomly with the help of indexes • It is easy to store and manipulate large data sets Disadvantages of Arrays in Java • The size of the array cannot be increased or decreased once it is declared—arrays have a fixed size • Java cannot store heterogeneous data. It can only store a single type of primitives • Now that we understand what Java arrays are- let us look at how arrays in Java are declared and defined.
  • 4. Define an Array in Java Arrays in Java are easy to define and declare. First, we have to define the array. The syntax for it is: Here, the type is int, String, double, or long. Var-name is the variable name of the array.
  • 5. byte a[]; short b[]; boolean booleanArray[]; long longArray[]; float floatArray[]; double doubleArray[]; char charArray[];
  • 6. Instantiating an Array in Java When an array is declared, only a reference of an array is created. To create or give memory to the array, you create an array like this: The general form of new as it applies to one- dimensional arrays appears as follows: type var-name[];//array declaration var-name = new type [size];
  • 7. Example: int intArray[]; //declaring array intArray = new int[20]; // allocating memory to array OR int[] intArray = new int[20]; // combining both statements in one
  • 8. Array Literal The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types). In a situation where the size of the array and variables of the array are already known, array literals can be used. int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literalThe length of this array determines the length of the created array.
  • 9. Types of Arrays: There are three types of arrays. We use these types of arrays as per the requirement of the program. 1.One-dimensional Array Also known as a linear array, the elements are stored in a single row. For example:
  • 10. 2. Two-dimensional Array Two-dimensional arrays store the data in rows and columns:
  • 11. class GFG { public static void main(String[] args) { // declares an Array of integers. int[] arr; // allocating memory for 5 integers. arr = new int[5]; // initialize the first elements of the array arr[0] = 10; // initialize the second elements of the array arr[1] = 20; // so on... arr[2] = 30; arr[3] = 40; arr[4] = 50; // accessing the elements of the specified array for (int i = 0; i < arr.length; i++) System.out.println("Element at index " + i + " : " + arr[i]); } }
  • 12. 3. Multi-dimensional Array This is a combination of two or more arrays or nested arrays. We can even use more than two rows and columns using the following code:
  • 13. //Java Program to illustrate the use of multidimensional array class Testarray3 { public static void main(String args[]) { //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } } }