Escolar Documentos
Profissional Documentos
Cultura Documentos
2018
2 Arrays
3 Section
4 StringBuffer
5 Scanner
6 StringTokenizer
7 Wrapper Classes
1
For each package/class/interface for constructors, methods and exception, please
refer to Java API documentation
Dr.Ami Tusharkant Choksi OOPJ(2150704) 2018 2 / 74
Syllabus
2
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Dr.Ami Tusharkant Choksi OOPJ(2150704) 2018 3 / 74
Arrays I
Declaration
1 int [] arrayname ; // declares an array of
integer
2 String [] names ; // String array
3 int [][] matrix ; // this is an array of
arrays or 2D array
Assign Size:
1 arrayname = new int [5]; // size 5
2 matrix = new int [5][5];
17 System.out.println("arr["+i+"]:" + arr[i]);
18 }
1 class ArrayBasics{
2 public static void main(String args[]){
3 int arr[] = new int[10];
4 for(int i=0;i<arr.length;i++){
5 //assign value to array elements
6 arr[i] = i+1;
7 //print value of array elements
8 System.out.println("arr["+i+"]:" + arr[i]);
9 }
10 }
11 }
12 /*
13 Output:
14 arr[0]:1
15 arr[1]:2
16 arr[2]:3
17 arr[3]:4
18 arr[4]:5
19 arr[5]:6
20 arr[6]:7
21 arr[7]:8
22 arr[8]:9
23 arr[9]:10
24 */
Program 1: Array Values Assign and Print Program
17
18 4. copyOf
19 import java.util.Arrays;
20 int[] a = {1,2,3,4,5};
21 int[] b = Arrays.copyOf(a, a.length);//import
java.util.Arrays required
22
23 5. Arrays.copyOfRange
24 int[] a = {1,2,3};
25 int[] b = Arrays.copyOfRange(a, 0, a.length);
26 a[0] = 5;
27 System.out.println(Arrays.toString(a)); //
[5,2,3]
1
2 import java.util.Arrays;
3
4 public class ArrayCopy {
5
6 public static void main(String args[]) {
7 int A[] = {1, 2, 3, 4, 5};
8 int B[] = new int[A.length];
9 System.out.println("Copying element by element
using for loop");
10 for (int i = 0; i < 5; i++) {
103 */
1 ALOAD 1
2 INVOKEVIRTUAL [I.clone ()Ljava/lang/Object;
3 CHECKCAST [I
4 ASTORE 2
1 class TwoDArrayAddition{
2 public static void main(String args[]){
3 int[][] arr1 = {{1,2,3},{4,5,6},{7,8,9}};
4 int arr2[][]={{1,2,3},{4,5,6},{7,8,9}};
5 int[][] resultarr[][]=new int[3][3];
6
7 for(int i=0;i<3;i++){
8 for(int j=0;j<3;j++){
9 //Addition of two arrays
1 class TwoDArrayMultiplication{
2 public static void main(String args[]){
3
4 int arr1[][] = {{1,2,3},{4,5,6},{7,8,9}};
5 int arr2[][]={{1,2,3},{4,5,6},{7,8,9}};
6
7 int r1 = arr1.length;
8 int c1 = arr1[0].length;
9
10 int r2 = arr2.length;
25 }
26 }
27 }
28 }
29 /*
30 30 36 42
31 66 81 96
32 102 126 150
33 */
Program 7: "Matrix Multiplication"
1 1. For loop
2 int src[][] = new int [3][3];
3 int dst[][]=new int[src.length][src[0].length];
4 for(int i=0;i<src.length;i++){
5 for(int j=0;j<src.length;j++){
6 dst[i][j] = src[i][j];
7 }
8
9 2.Cloning
10 int A[][] = {{1, 2}, {3, 4,}, {5,6}};
11 int B[][] = A.clone();
12 //new int[A.length][A[0].length];
13 for(int i=0;i<B.length;i++)
14 {
15 System.out.println(Arrays.toString(B[i]));
32 }
33
34 5.Arrays.copyOfRange
35 String[][] sss = { {"1", "2"}, {"3", "4"},{"5","
6"}};
36 String[][] sss1 = Arrays.copyOfRange(sss, 0, 3);
37 for(int i=0;i<sss1.length;i++){
38 System.out.println(Arrays.toString(sss1[i]));
39 }
167 c
168 t
169 i
170 O
171 n
172 Substring : substring
173 Starts with : true
174 Ends with : true
175 Replaced string : The domestic pari is a small
, typically furry , carnivorous mammal . They
are often called house cats when kept as
indoor pets or simply cats when there is no
need to distinguish them from other felids
and felines .
176 the index of pari : 13
1 /*
2 $terminal$: javac CommandLineArgs.java
3 $terminal$: java CommandLineArgs ami choksi
vikramaditya bhoj
4 args[0]: ami
5 args[1]: choksi
6 args[2]: vikramaditya
7 args[3]: bhoj
8 */
Program 11: Commandline Arguments Passing and Printing
1 import java.util.Scanner;
2 public class ScannerDemo {
3 public static void main(String[] args) {
4 Scanner input = new Scanner(System.in);
5 //input int no
6 System.out.println("Input int no");
7 int no = input.nextInt();
8 System.out.println("Int no: " + no);
9 //input int no in binary
Dr.Ami Tusharkant Choksi OOPJ(2150704) 2018 61 / 74
Scanner-Input Text from Standard Input II
10 System.out.println("Input binary int no");
11 int bno = input.nextInt(2);
12 System.out.println("bno: " + bno);
13 //input double no
14 System.out.println("Input double no");
15 double dno = input.nextDouble();
16 System.out.println("Double no: " + dno);
17 //input text
18 System.out.println("Input text");
19 String inString = input.next();
20 System.out.println("String : " + inString);
21 //input line
22 System.out.println("Input a line");
23 String line = input.nextLine();
24 System.out.println("Line : " + line);
25 //input boolean
Dr.Ami Tusharkant Choksi OOPJ(2150704) 2018 62 / 74
Scanner-Input Text from Standard Input III
26 System.out.println("Input boolean(true/false)");
27 boolean b = input.nextBoolean();
28 System.out.println("Boolean : " + b);
29 }
30 }
31 /*
32 Output:
33 Input int no
34 5
35 Int no: 5
36 Input binary int no
37 1010
38 bno: 10
39 Input double no
40 67.8
41 Double no: 67.8
Dr.Ami Tusharkant Choksi OOPJ(2150704) 2018 63 / 74
Scanner-Input Text from Standard Input IV
42 Input text
43 ami tusharkant Choksi
44 String : ami
45 Input a line
46 Line : tusharkant Choksi
47 Input boolean(true/false)
48 true
49 Boolean : true
50 */
1 import java.util.StringTokenizer;
2 public class StringTokenizerDemo {
3 public static void main(String[] args) {
4 String s = "ami-choksi-associate professor-phd-
surat";
5 StringTokenizer st = new StringTokenizer(s, "-")
;
Dr.Ami Tusharkant Choksi OOPJ(2150704) 2018 66 / 74
StringTokenizer III
6 int i = 1;
7 while (st.hasMoreTokens()) {
8 System.out.println("Token " + i++ + ": " + st.
nextToken());
9 }
10 }
11 }
12 /*
13 Output:
14 Token 1: ami
15 Token 2: choksi
16 Token 3: associate professor
17 Token 4: phd
18 Token 5: surat
19 */
Program 12: StringTokenizer Demo
39 parseByte : 12
40 intValue : 40
41 doubleValue : 0.5
42 booleanValue : true
43 byteValue : 0
44 */
Program 13: Wrapper Classes Demo