SlideShare a Scribd company logo
1 of 19
Arrays
Array:
An array is a container object that holds a fixed
number of values of a single type.
The length of an array is established when the array is created.
After creation, its length is fixed.
79 87 94 82 67 98 87 81 74 91
0 1 2 3 4 5 6 7 8 9
scores
The entire array
has a single name
Each value has a numeric index
Ex:
int india_score = 200;
int pak_score = 190;
int aus_score = 210;
int srilanka_score = 195;
System.out.println("India = " + india_score);
System.out.println("Pak = " + pak_score);
System.out.println("Aus = " + aus_score);
System.out.println("Sri Lanka = " +
srilanka_score);
Ex:
class PrintTeamScores
{
public static void main(String arg[])
{
int[] scores = new int[4]; // LINE A - Creating the scores array.
scores[0] = 200; // assigning score for team 0 or India
scores[1] = 190; // assigning score for team 1 or Pakistan
scores[2] = 210; // assigning score for team 2 or Australia
scores[3] = 195; // assigning score for team 3 or Sri Lanka
System.out.println("India = " + scores[0]);
System.out.println("Pak = " + scores[1]);
System.out.println("Aus = " + scores[2]);
System.out.println("Sri Lanka = " + scores[3]);
}
}
One-Dimensional Array
 A List of items can be given one variable name using only one
Subscript and such variable is called One-Dimensional Array.
 The Subscript always begin with number 0
i.e., x[ 0 ].
 We Create variable size as
int num [ ] = new int [ 12 ];
Creating an Array
 Creation of an Array involves 3 types
1.Declaring the Array.
2.Creating Memory Locations.
3.Initialization of Arrays.
Array Declartion
Array in java Declared in Two Forms
Form 1: type arrayname[ ];
Ex:
Form 2: type[ ]arrayname;
Ex:
Int number[ ];
Int average[ ];
Int [ ] counter;
Int[ ] marks;
Creation of Memory Allocation
 Java allows us to create arrays using new operator only.
arrayname = new type[ size ];
Ex:
int number = new int [ 12 ];
float average = new float[ 5 ];
Initialization of Arrays.
 Assigning the values into Arrays. This is
known as Initialization.
 This is done using Array SubScripts.
arrayname[ SubScripts ] = value;
Ex:
number[ 0 ] = 46;
number[ 1 ] = 44;
number[ 2 ] = 37;
 Array Initializer is a list of values seperated by Comas and
Surrounded by Curly braces.
 No size is given.
Ex: int number[ ]= {10,20,30,40};
 It is also possible to assign an array object to another.
Ex: int a[ ]={10,20,30};
int b[ ];
b=a;
Strings
 Character array:
char charArray[ ]= new char[ 2 ];
charArray[ 0 ]=‘H’;
charArray[ 1 ]=‘I’;
 Java Strings are more reliable and predictable
 This is bascially due to C’s lack of bounds checking
 Java String is not Character Array.
Arrays in Strings
 Normally, objects in Java are created with the new keyword.
 However, String objects can be created "implicitly":
 Strings can also be created using the + operator. The +
operator, when applied to Strings means concatenation
String name;
name = new String("Car");
String name;
name = "Car";
int age = 21;
String message = "Car has " + “Engine";
Immutable String
 string objects are immutable. Immutable simply means unmodifiable or
unchangeable.
 Once string object is created its data or state can't be changed but a new
string object is created.
class Testimmutablestring
{
public static void main(String args[])
{
String s="Sachin";
s.concat(" Tendulkar");//concat() method appends the string at the end
System.out.println(s);//will print Sachin because strings are immutable objects
}
}
 There are multiple ways to initialize a String Array.
 Initialization can also be done at the same time as the declaration.
String[ ] StrArray = {"AAA", "BBB", "CCC", "DDD", "EEE"};
 This will create a String Array of length 5. Element at index 0 will
have the value "AAA", element at index 1 will have the value
"BBB", and so on.
String[] StrArray = {"AAA", "BBB", "CCC", "DDD", "EEE"};
System.out.println(StrArray[0] );
System.out.println(StrArray[1] );
System.out.println( StrArray[2] );
System.out.println( StrArray[3] );
System.out.println( StrArray[4] );
 We can also create and use arrays that contain strings.
String[] StrArray = new String[4];
 The statement will create an StringArray of size 5 to hold 5
string constants.
private void StringArray()
{
StrArray [0] = “Jack”;;
StrArray [1] = “Mayn”;
StrArray[2] = “Aryan”;
StrArray[3] = “Arav”;
}
String Buffer
 Java StringBuffer class is used to created mutable (modifiable)
string.
 The StringBuffer class in java is same as String class except it is
mutable i.e. it can be changed.
 Java StringBuffer class is thread-safe i.e. multiple threads cannot
access it simultaneously.
 So it is safe and will result in an order.
StringBuffer Methods
Method Task
S1.SetCharAt(n,’x’) Modifies the nth character to x.
S1.append(s2) Appends the string s2 to s1 at the end
S1.insert(n,s2) Inserts the string s2 at the position n of the
String s1.
S1.setLength(n) Sets the length of the string s1 to n.
If n<s1.length() s1 is truncated.
sb.deleteCharAt(n) Deletes character at nth position
sb.reverse() IT prints reverse of the given string.
sb.substring(1,4) Extracts a substring from a string

More Related Content

What's hot (20)

ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Array in c#
Array in c#Array in c#
Array in c#
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Constants in java
Constants in javaConstants in java
Constants in java
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
String in java
String in javaString in java
String in java
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Java arrays
Java arraysJava arrays
Java arrays
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 

Similar to Arrays in java

Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxrohinitalekar1
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In Generalmartha leon
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................suchitrapoojari984
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injavairdginfo
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arraysJayanthiM19
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxJumanneChiyanda
 
Core Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysCore Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysWebStackAcademy
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)teach4uin
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.pptcoding9
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...ssuser6478a8
 

Similar to Arrays in java (20)

Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Array
ArrayArray
Array
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Core Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysCore Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - Arrays
 
L10 array
L10 arrayL10 array
L10 array
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
 

Recently uploaded

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
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
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 

Recently uploaded (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
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
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 

Arrays in java

  • 2. Array: An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. 79 87 94 82 67 98 87 81 74 91 0 1 2 3 4 5 6 7 8 9 scores The entire array has a single name Each value has a numeric index
  • 3. Ex: int india_score = 200; int pak_score = 190; int aus_score = 210; int srilanka_score = 195; System.out.println("India = " + india_score); System.out.println("Pak = " + pak_score); System.out.println("Aus = " + aus_score); System.out.println("Sri Lanka = " + srilanka_score);
  • 4. Ex: class PrintTeamScores { public static void main(String arg[]) { int[] scores = new int[4]; // LINE A - Creating the scores array. scores[0] = 200; // assigning score for team 0 or India scores[1] = 190; // assigning score for team 1 or Pakistan scores[2] = 210; // assigning score for team 2 or Australia scores[3] = 195; // assigning score for team 3 or Sri Lanka System.out.println("India = " + scores[0]); System.out.println("Pak = " + scores[1]); System.out.println("Aus = " + scores[2]); System.out.println("Sri Lanka = " + scores[3]); } }
  • 5. One-Dimensional Array  A List of items can be given one variable name using only one Subscript and such variable is called One-Dimensional Array.  The Subscript always begin with number 0 i.e., x[ 0 ].  We Create variable size as int num [ ] = new int [ 12 ];
  • 6. Creating an Array  Creation of an Array involves 3 types 1.Declaring the Array. 2.Creating Memory Locations. 3.Initialization of Arrays.
  • 7. Array Declartion Array in java Declared in Two Forms Form 1: type arrayname[ ]; Ex: Form 2: type[ ]arrayname; Ex: Int number[ ]; Int average[ ]; Int [ ] counter; Int[ ] marks;
  • 8. Creation of Memory Allocation  Java allows us to create arrays using new operator only. arrayname = new type[ size ]; Ex: int number = new int [ 12 ]; float average = new float[ 5 ];
  • 9. Initialization of Arrays.  Assigning the values into Arrays. This is known as Initialization.  This is done using Array SubScripts. arrayname[ SubScripts ] = value; Ex: number[ 0 ] = 46; number[ 1 ] = 44; number[ 2 ] = 37;
  • 10.  Array Initializer is a list of values seperated by Comas and Surrounded by Curly braces.  No size is given. Ex: int number[ ]= {10,20,30,40};  It is also possible to assign an array object to another. Ex: int a[ ]={10,20,30}; int b[ ]; b=a;
  • 11.
  • 12. Strings  Character array: char charArray[ ]= new char[ 2 ]; charArray[ 0 ]=‘H’; charArray[ 1 ]=‘I’;  Java Strings are more reliable and predictable  This is bascially due to C’s lack of bounds checking  Java String is not Character Array.
  • 13. Arrays in Strings  Normally, objects in Java are created with the new keyword.  However, String objects can be created "implicitly":  Strings can also be created using the + operator. The + operator, when applied to Strings means concatenation String name; name = new String("Car"); String name; name = "Car"; int age = 21; String message = "Car has " + “Engine";
  • 14. Immutable String  string objects are immutable. Immutable simply means unmodifiable or unchangeable.  Once string object is created its data or state can't be changed but a new string object is created. class Testimmutablestring { public static void main(String args[]) { String s="Sachin"; s.concat(" Tendulkar");//concat() method appends the string at the end System.out.println(s);//will print Sachin because strings are immutable objects } }
  • 15.  There are multiple ways to initialize a String Array.  Initialization can also be done at the same time as the declaration. String[ ] StrArray = {"AAA", "BBB", "CCC", "DDD", "EEE"};  This will create a String Array of length 5. Element at index 0 will have the value "AAA", element at index 1 will have the value "BBB", and so on. String[] StrArray = {"AAA", "BBB", "CCC", "DDD", "EEE"}; System.out.println(StrArray[0] ); System.out.println(StrArray[1] ); System.out.println( StrArray[2] ); System.out.println( StrArray[3] ); System.out.println( StrArray[4] );
  • 16.  We can also create and use arrays that contain strings. String[] StrArray = new String[4];  The statement will create an StringArray of size 5 to hold 5 string constants. private void StringArray() { StrArray [0] = “Jack”;; StrArray [1] = “Mayn”; StrArray[2] = “Aryan”; StrArray[3] = “Arav”; }
  • 17.
  • 18. String Buffer  Java StringBuffer class is used to created mutable (modifiable) string.  The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.  Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously.  So it is safe and will result in an order.
  • 19. StringBuffer Methods Method Task S1.SetCharAt(n,’x’) Modifies the nth character to x. S1.append(s2) Appends the string s2 to s1 at the end S1.insert(n,s2) Inserts the string s2 at the position n of the String s1. S1.setLength(n) Sets the length of the string s1 to n. If n<s1.length() s1 is truncated. sb.deleteCharAt(n) Deletes character at nth position sb.reverse() IT prints reverse of the given string. sb.substring(1,4) Extracts a substring from a string