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 in java with in details presentation.ppt
PPT
ARRAYSinJavaProgramming_Introduction.ppt
PPT
ARRAYS.ppt
PPT
ARRAYS.ppt
PPTX
Chap1 array
PPTX
PPTX
Chapter 7.1
ARRAYS in java with in details presentation.ppt
ARRAYSinJavaProgramming_Introduction.ppt
ARRAYS.ppt
ARRAYS.ppt
Chap1 array
Chapter 7.1

Similar to Array and 2D Array and with syntax working (20)

PPTX
Computer programming 2 Lesson 13
PPTX
Arrays in Java with example and types of array.pptx
PPTX
Arrays.pptx
PPT
Comp102 lec 8
PPT
17-Arrays en java presentación documento
PPTX
Module 7 : Arrays
PPT
Multi dimensional arrays
PDF
Learn Java Part 9
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PPT
7.basic array
PDF
Lecture 6 - Arrays
PDF
Arrays a detailed explanation and presentation
PDF
Lecture 2.8 Arrays.pdf
PPTX
Array lecture
PPT
Arrays Basicfundamentaldatastructure.ppt
PDF
Java arrays (1)
PPTX
Unit-2.Arrays and Strings.pptx.................
Computer programming 2 Lesson 13
Arrays in Java with example and types of array.pptx
Arrays.pptx
Comp102 lec 8
17-Arrays en java presentación documento
Module 7 : Arrays
Multi dimensional arrays
Learn Java Part 9
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
7.basic array
Lecture 6 - Arrays
Arrays a detailed explanation and presentation
Lecture 2.8 Arrays.pdf
Array lecture
Arrays Basicfundamentaldatastructure.ppt
Java arrays (1)
Unit-2.Arrays and Strings.pptx.................
Ad

Recently uploaded (20)

PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
Trading Procedures (1).pptxcffcdddxxddsss
PDF
Linux OS guide to know, operate. Linux Filesystem, command, users and system
PPTX
Challenges and opportunities in feeding a growing population
PPTX
LESSON-1-NATURE-OF-MATHEMATICS.pptx patterns
PPTX
Azure Data management Engineer project.pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Moving the Public Sector (Government) to a Digital Adoption
PPTX
artificial intelligence deeplearning-200712115616.pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Presentation1.pptxvhhh. H ycycyyccycycvvv
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
lec_5(probability).pptxzzjsjsjsjsjsjjsjjssj
PDF
Foundation of Data Science unit number two notes
PPTX
Extract Transformation Load (3) (1).pptx
PDF
Data Analyst Certificate Programs for Beginners | IABAC
PDF
Chad Readey - An Independent Thinker
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
STUDY DESIGN details- Lt Col Maksud (21).pptx
Trading Procedures (1).pptxcffcdddxxddsss
Linux OS guide to know, operate. Linux Filesystem, command, users and system
Challenges and opportunities in feeding a growing population
LESSON-1-NATURE-OF-MATHEMATICS.pptx patterns
Azure Data management Engineer project.pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Moving the Public Sector (Government) to a Digital Adoption
artificial intelligence deeplearning-200712115616.pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Presentation1.pptxvhhh. H ycycyyccycycvvv
Miokarditis (Inflamasi pada Otot Jantung)
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
lec_5(probability).pptxzzjsjsjsjsjsjjsjjssj
Foundation of Data Science unit number two notes
Extract Transformation Load (3) (1).pptx
Data Analyst Certificate Programs for Beginners | IABAC
Chad Readey - An Independent Thinker
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Ad

Array and 2D Array and with syntax working

  • 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