Java HariKrishna
Hari Krishna is a Sr. Java Trainer working with Naresh i Technologies
Tutorial Site: javaharikrishna.com
12/01/2023
15/03/2022
Assignments on Array Programming[Part-1]
===========================================
You must develop below program by using both tradition approach by using for loop and by using Java 8v Stream API approach
===========================================
1. Develop a program to create an array of integers, display all values on console with their position
starts with 1. Support if an array has values 3, 4, 5, 6, 7 you must display values as
Value 1: 3
Value 2: 4
Value 3: 5
Value 4: 6
Value 5: 7
2. Develop a program to create an array of integers, display only even indexes elements
Support if an array has values 3, 4, 5, 6, 7 you must display values "even index" values 3 5 7
0 1 2 3 4
3. Develop a program to create an array of integers, display only even elements
Support if an array has values 3, 4, 5, 6, 7 you must display values "even elements" 4 6
0 1 2 3 4
4. Develop a program to create an array of integers, display only the elements divisible by 3
Support if an array has values 3, 4, 5, 6, 7 you must display values "/ 3 " 3, 6
0 1 2 3 4
5. Develop a program to create an array of integers, search for the element 2,
if available display "the value 2 is available"
if no available display "the value 2 is not available"
Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: the value 2 is not available
Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: the value 2 is available
6. Develop a program to create an array of integers, search for the element 2,
if available display "the value 2 is available"
if no available display "the value 2 is not available"
Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: the value 2 is available
Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: the value 2 is not available
7. Develop a program to create an array of integers, search for the element 2,
if available display "its index number" to tell that value 2 is present in array at so and so location
if not available display "-1" to tell that value 2 is not present in array
Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: 3
Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: -1
7. Develop a program to create an array of integers, REMOVE the element 2, FROM THE ARRAY
Support if an array has values 3, 4, 5, 2, 6 after remove 2 array must be
O/P: [3, 4, 5, 2, 6]
O/P: [3, 4, 5, 6]
8. Develop a program to create an array of integers, INSERT the element 2 in this ARRAY at 3rd index
Support if an array has values 3, 4, 5, 6 after inserting 2 array must be
O/P: [3, 4, 5, 6]
O/P: [3, 4, 5, 2, 6]
9. Develop a program to create an array of integers, REPALCE the element 2 with 9
Support if an array has values 3, 4, 5, 2, 6 after replacing 2 array must be
O/P: [3, 4, 5, 2, 6]
O/P: [3, 4, 5, 9, 6]
10. Develop a program to create an array of integers, SORT the elements in array in Ascending order
Support if an array has values 3, 4, 5, 2, 6 after sorting array must
O/P: [3, 4, 5, 2, 6]
O/P: [2, 3, 4, 5, 6]
14/03/2022
class Test18_ArrayRules {
public static void main(String[] args) {
//1. Declaring an array (1D and 2D)
//2. Creating an array object (1D and 2D with three syntaxes)
//3. Initializing and modifying array object
//4. Reading array object values
//================== Rules on declaring an array ========================
//Rule #1: We can place [] after DT, before VN, after VN,
//but we can not place before DT
int[] ia1;
int []ia2; //=> int[] ia2;
int ia3[];
//[]int ia4;
int[]ia5;
int[][] ia6;
int [][]ia7; //int[][] ia7;
int ia8[][];
//[][]int ia9;
int[][]ia10;
int[] ia11[];
int[] []ia12[]; //int[][] ia12[];
//What is the difference in placing [] after DT and after variable name?
int[] ia13; //DT is int[] and VT int[]
int ia14[]; //DT is int and VT int[]
int[] ia15, ia16; //DT is int[] and both ia15 and ia16 are int[]
int ia17[], ia18; //DT is int and ia17 is int[] and ia18 is int
ia17 = new int[5];
ia18 = 7;
//ia17 = 7;
//ia18 = new int[5];
//Q1) What will be happened if we place [] before variable name?
//It will be attached to DT
//Q2) Can we create array type variable and normal type variable in a single statement?
//A) Yes, but by data type will be same
int ia19, ia20[];
int ia21[], ia22[];
int[] ia23, ia24;
//Rule #2: we can not palce [] before second variable onwards
// we are allowed to place only before first variable
int []ia25;
int []ia26, ia27; //both are array type variables, because [] will be attached to DT
//ia25 = 10;
//ia26 = 11;
//int ia28, []ia29; //CE:
int[][] ia30, ia31; //both are 2d arrays
int[] ia32[], ia33; //ia32 is 2D and ia33 is 1D
int[] ia34, ia35[]; //ia34 is 1D and ia35 is 2D
int[] []ia36, ia37; //both are 2D arrays
//int[] ia38, []ia39; //CE:
//Rule #3: Like in C langauge, in Java we can not specify size in array declaration side
// size is allowed only in object creation side
//int[5] ia40;
int[] ia41;
//======================= Ruels on creating an array object ===================
//DT[] vN = new DT[size];
//DT[] vN = {v1, v2, v3, ....};
//DT[] vN = new DT[]{v1, v2, v3, ....};
//============ Rule based on DT[] vN = new DT[size]; array object creation=============
//Rule #4: We must specify size of the array in array object creation side
//int[] ia42 = new int[]; //CE: array dimension is missing
//Rule #5: Array size is int type, so we must pass eithe int or char or byte or shor type values only
//if we pass other types like long, float, double, boolean or String we will get CE: i c t
int[] ia43 = new int[5]; //array with 5 locations
int[] ia44 = new int['a']; //array with 97 locations
//int[] ia45 = new int[5L]; //CE
//int[] ia45 = new int[5F]; //CE
//int[] ia45 = new int[5D]; //CE
//int[] ia45 = new int[true]; //CE
//int[] ia45 = new int["a"]; //CE
//You create array object of any type, its size must be int or its lesser range values
//long[] la = new long[5L];
//String[] sa = new String["abc"];
//Example[] ea = new Example[5.7];
//boolean[] ba = new boolean[true];
boolean[] ba = new boolean[5];
//boolean[3] ba = new boolean[5];
//boolean[5] ba = new boolean[5];
//Rule #6: Size must be +ve int or its lesser range value
//if we pass -ve value as size, we won't get CE, but we will get RE: NASE
//int[] ia46 = new int[-5];
//Rule #7: like PDT, their array types are not compatible
//if we assign one PDT array to another PDT array, we will get CE:
int i1 = 'a';
long l1 = i1;
//boolean bo = l1;
int[] ia47 = new int[5];
//long[] ia48 = new int[5];
//int[] ia49 = new char[5];
//============ Rule based on DT[] vN = {v1, v2, v3, ...}; array object creation=============
//Rule #8: inside {}, we are allowed to place values of either same type or lesser type of this array type
int[] ia50 = {}; //0 locations array, empty array
int[] ia51 = {3}; //1 location array, with value 3
//int[] ia52 = {3, 4L};
int[] ia53 = {3, 'a'};
//============ Rule based on DT[] vN = new int[]{v1, v2, v3, ...}; array object creation=============
//Rule #9: We can not specify size here in this syntax
int[] ia54 = new int[]{}; //size=0, number of values we specified in {}
int[] ia55 = new int[]{3}; //size=1, number of values we specified in {}
//int[] ia55 = new int[3]{}; //CE: array creation with both dimension expression and initialization is illegal
//Note: because this sytax is combination of both 1st and 2nd syntax those two syntaxes
//rules are also applied on this syntaxes. That means we must applied total 6 rules from #4 to #9
//================== Rules on MD array =======================
//Rule #10: In MD array creation parent array size is mandatory
//if we donot mension child array size no CE, child arrays are not created.
//int[][] ia56 = new int[][];
int[][] ia57 = new int[3][2];
int[][] ia58 = new int[3][];
//int[][] ia59 = new int[][2];
//Rule #11:
int[][] ia59 = {}; //0 location parent array
int[][] ia591 = {{}}; //1 parent array and 1 child array
//1 location parent array and zero locations child array
int[][] ia60 = {{3}}; //1 parent array and 1 child array
//1 location parent array and 1 location child array with the value 3
int[][] ia61 = {{3, 4}}; //1 parent array and 1 child array
//1 location parent array and 2 locations child array with the values 3 and 4
int[][] ia62 = {{3, }}; //1 location parent array and 1 location child array with the value 3
System.out.println(ia62[0][0]); //3
//System.out.println(ia62[0][1]); //RE: AIOOBE
//int[][] ia63 = {{3, ,}}; //1 location parent array and 1 location child array with the value 3
int[][] ia64 = {{3, 0,}}; //1 location parent array and 1 location child array with the value 3
System.out.println(ia64[0][0]); //3
System.out.println(ia64[0][1]); //0
//System.out.println(ia64[0][2]); //RE:
int[][] ia65 = {{3}, };
System.out.println(ia65[0]); //[I
//System.out.println(ia65[1]); //RE:
int[][] ia66 = {{3}, {}}; //2 locatons with 2 child arrays
System.out.println(ia66[0]); //[I
System.out.println(ia66[1]); //[I
int[][] ia67 = {{3}, null}; //2 locations with 1 child array
System.out.println(ia67[0]); //[I
System.out.println(ia67[1]); //null
System.out.println(ia67[0][0]); //3
System.out.println(ia67[1][0]); //RE:
//3 locations with 0 and 2 locations with child array
//int[][] ia68 = {{} , , {} };
int[][] ia69 = {{} , null , {} };
//int[][] ia70 = {3, 4}; //2d array with int values. not allowed
//int[][] ia71 = {{3}, 4}; //2d array with int[] and int value, not allowed
int[][] ia72 = {{3}, null}; //2d array with int[] value and null, allowed
int[][] ia73 = {null, null}; //2d array with 2 locations with two nulls, no child arrays
int[][] ia74 = {{}, {}}; //2d array with 2 locations with two child arrays, empty child arrays, no locations
int[][] ia75 = {{}, null, {}}; //2d array with 2 locations with two child arrays, empty child arrays, no locations
}
}
12/03/2022
/*
* Types of arrays
* Java supports three types of arrays
* 1. One dimensional array
* 2. Multi dimensional array [2D, 3D, ..., n D]
* 3. Jogged array [multi dimensional array with diff size child arrays]
*/
class Test15_TyoeOfArrays{
public static void main(String[] args){
//array of values
int[] ia1 = new int[5];
int[] ia2 = {3, 4, 5, 6, 7};
//array of arrays or 2D array
//DT[][] vn = new DT[parentSize][childSize];
// M O
int[][] iaa = new int[3][2];
//parent array locations -> 3
//child array locations -> 2
//how many parent arrays -> 1
//how many child arrays -> 3 with 2 locations
System.out.println(iaa);
System.out.print(" "+iaa[0] +" ----> "); System.out.println(iaa[0][0] +" " + iaa[0][1]);
System.out.print(" "+iaa[1] +" ----> "); System.out.println(iaa[1][0] +" " + iaa[1][1]);
System.out.print(" "+iaa[2] +" ----> "); System.out.println(iaa[2][0] +" " + iaa[2][1]);
System.out.println();
long time1, time2;
time1 = System.currentTimeMillis();
for(int i=0;i
11/03/2022
Fail-Fast and Fail-Safe cursor objects
==========================
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
/*
* The cursor that does not allow concurrent modifications on a collection object
* is called fail-fast cursor. Enumeration and Iterator are FF cursor on CF collections
*
* The cursor that allows concurrent modifications on a collection object
* is called fail-safe cursor. Iterator on concurrent collection given in
* java.util.concurrent package is fail safe cursor. because these collections
* are by default thread safe means concurrent collections
*
* Enumeration on Vector through legacy implementation with elements() method is
* fail safe, because Vector is thread safe, but Enumeration on Vector through
* Collections.enumeration(v) is fail fast because it implicitly uses Iterator
*
* Iterator on Vector is also fail fast
*
*/
public class Test24_FFnFS {
public static void main(String[] args) {
//-------------------------------------------------------------------------------
ArrayList al = new ArrayList();
al.add("a");
al.add("b");
al.add("c");
System.out.println(al);
Iterator itr = al.iterator();
al.add("d");
//System.out.println(itr.next()); //ITR on CF AL is FF hence we got CME
//-------------------------------------------------------------------------------
CopyOnWriteArrayList cowal = new CopyOnWriteArrayList();
cowal.add("a");
cowal.add("b");
cowal.add("c");
System.out.println(cowal);
itr = cowal.iterator();
cowal.add("d");
System.out.println(itr.next()); //ITR COWAL is FS hence we did not get CME
System.out.println(cowal);
System.out.println();
//-------------------------------------------------------------------------------
Vector v = new Vector();
v.add("a");
v.add("b");
v.add("C");
System.out.println(v);
Enumeration e = v.elements(); //Enumeration through elements() is FS
v.add("d"); //on Vector, because Vector is thred safe
System.out.println(e.nextElement());
System.out.println(v);
itr = v.iterator();
v.add("e");
//System.out.println(itr.next()); //Iterator is FF on Vector
//even though it Thread Safe
e = Collections.enumeration(v);
v.add("f");
System.out.println(e.nextElement()); //Enumeration with Collection.enumeration(v)
//is FF because implicitly it uses Iterator
}
}
Click here to claim your Sponsored Listing.
Category
Contact the business
Telephone
Website
Address
Hyderabad
500016