Array In Java Created by Manish Tiwari
Presentation By Manish Tiwari, Assistant Professor, Software Engineer, Computer Science and Application
2. WhatsApp: ? 4. Blog: manishtiwarise.blogspot.in 3. Email: tiwarikmanish@gmail.com 1. Twitter: @manishtiwarise 5. SlideShare: https://www.slideshare.net/tiwarikmanish Find Notes
A R R A Y S 0 1 2 3 4 5 Element Index
Array is a collection of similar type(Homogeneous) of elements stored in contiguous memory location. Definition of Java Array 1. Code Optimization: It makes the code optimized, we can retrieve or sort the data easily. 2. Random access: We can get any data located at any index position. Advantage of Java Array 1. Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java. Disadvantage of Java Array
1. Single Dimensional Array Syntax: // preferred way. 1. DataType[] ArrayName; Example: double[] myList; 2. DataType ArrayName[]; Example: double myList[]; Types of Java Array 2. Multidimensional Array Syntax: // preferred way. 1. dataType[][] arrayRefVar; (or) 2. dataType [][]arrayRefVar; (or) 3. dataType arrayRefVar[][]; (or) 4. dataType []arrayRefVar[]; 0 There are Two Types of Array.
class Testarray { public static void main(String args[]) { int a[]=new int[5];//declaration and instantiation a[0]=10;//initialization a[1]=20; a[2]=70; a[3]=40; a[4]=50; //printing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); } } Program-Single Dimensional Array
class Testarray1 { public static void main(String args[]) { int a[]={33,3,4,5}; //declaration, instantiation and initialization //printing array for(int i=0;i<a.length;i++) //length is the property of array System.out.println(a[i]); } } Declaration, Instantiation and Initialization of Java Array
class Testarray5 { public static void main(String args[]){ int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; int c[][]=new int[2][3]; for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j]+" "); } System.out.println();//new line } } } Adding 2 Matrices Using Multi Dimensional
String is a sequence of characters. But in java, string is an object that represents a sequence of characters. The java.lang.String class is used to create string object. String in Java String Types There are two ways to create String object: 1.By string literal String s="welcome"; 2. By new keyword String s=new String("Welcome");
String Program public class StringExample { public static void main(String args[]) { String s1="java"; char ch[]={'s','t','r','i','n','g','s'}; String s2=new String(ch); String s3=new String("example"); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s1.toUpperCase()); System.out.println(s1.toLowerCase()); System.out.println(s2.charAt(3)); System.out.println(s2.length()); System.out.println(s3.trim()); System.out.println(s3.startsWith(“s"));//true System.out.println(s1.endsWith(“s"));//true } }

Java Array String

  • 1.
    Array In Java Createdby Manish Tiwari
  • 2.
    Presentation By Manish Tiwari, AssistantProfessor, Software Engineer, Computer Science and Application
  • 3.
    2. WhatsApp: ? 4.Blog: manishtiwarise.blogspot.in 3. Email: tiwarikmanish@gmail.com 1. Twitter: @manishtiwarise 5. SlideShare: https://www.slideshare.net/tiwarikmanish Find Notes
  • 4.
    A R RA Y S 0 1 2 3 4 5 Element Index
  • 5.
    Array is acollection of similar type(Homogeneous) of elements stored in contiguous memory location. Definition of Java Array 1. Code Optimization: It makes the code optimized, we can retrieve or sort the data easily. 2. Random access: We can get any data located at any index position. Advantage of Java Array 1. Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java. Disadvantage of Java Array
  • 6.
    1. Single DimensionalArray Syntax: // preferred way. 1. DataType[] ArrayName; Example: double[] myList; 2. DataType ArrayName[]; Example: double myList[]; Types of Java Array 2. Multidimensional Array Syntax: // preferred way. 1. dataType[][] arrayRefVar; (or) 2. dataType [][]arrayRefVar; (or) 3. dataType arrayRefVar[][]; (or) 4. dataType []arrayRefVar[]; 0 There are Two Types of Array.
  • 7.
    class Testarray { public staticvoid main(String args[]) { int a[]=new int[5];//declaration and instantiation a[0]=10;//initialization a[1]=20; a[2]=70; a[3]=40; a[4]=50; //printing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); } } Program-Single Dimensional Array
  • 8.
    class Testarray1 { public staticvoid main(String args[]) { int a[]={33,3,4,5}; //declaration, instantiation and initialization //printing array for(int i=0;i<a.length;i++) //length is the property of array System.out.println(a[i]); } } Declaration, Instantiation and Initialization of Java Array
  • 9.
    class Testarray5 { public staticvoid main(String args[]){ int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; int c[][]=new int[2][3]; for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j]+" "); } System.out.println();//new line } } } Adding 2 Matrices Using Multi Dimensional
  • 10.
    String is asequence of characters. But in java, string is an object that represents a sequence of characters. The java.lang.String class is used to create string object. String in Java String Types There are two ways to create String object: 1.By string literal String s="welcome"; 2. By new keyword String s=new String("Welcome");
  • 11.
    String Program public classStringExample { public static void main(String args[]) { String s1="java"; char ch[]={'s','t','r','i','n','g','s'}; String s2=new String(ch); String s3=new String("example"); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s1.toUpperCase()); System.out.println(s1.toLowerCase()); System.out.println(s2.charAt(3)); System.out.println(s2.length()); System.out.println(s3.trim()); System.out.println(s3.startsWith(“s"));//true System.out.println(s1.endsWith(“s"));//true } }