Skip to content

Commit 14d6900

Browse files
lajitlajit
authored andcommitted
Updated java programs - 14th Aug20
1 parent a2ee72b commit 14d6900

File tree

7 files changed

+190
-7
lines changed

7 files changed

+190
-7
lines changed

JavaBasicPrograms/src/main/java/learnConditionalStatemt/SwitchCaseExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class SwitchCaseExample {
66

77
public void checkmyHero() {
88

9-
switch (myHero) {
9+
/* switch (myHero) {
1010
case "Iron Man":
1111
System.out.println("My hero is Iron Man");
1212
break; // If we dont give break; command, all other following cases will also get executed without checking the conditions
@@ -22,7 +22,7 @@ public void checkmyHero() {
2222
default:
2323
System.out.println("My hero is Bat Man");
2424
25-
}
25+
} */
2626

2727
}
2828

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package learnList;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.ListIterator;
7+
8+
public class ArrayListExample {
9+
10+
public static void main(String[] args) {
11+
List<String> arraylist1 = new ArrayList<String>(); // using Dynamic Polymorphism
12+
13+
arraylist1.add("honda");
14+
arraylist1.add("TATA");
15+
arraylist1.add("Swift");
16+
arraylist1.add("BMW");
17+
arraylist1.add("BMW");
18+
arraylist1.add("TATA");
19+
System.out.println("CAR list is: "+ arraylist1); // string values added in the insertion order & allowed duplicates
20+
21+
System.out.println(arraylist1.get(2)); //get the index value of 2 - Swift
22+
System.out.println(arraylist1.indexOf("TATA")); //get the index value
23+
System.out.println(arraylist1.lastIndexOf("BMW")); //get the last index value of the string given
24+
25+
List<String> anotherList = new ArrayList<String>();
26+
anotherList.addAll(arraylist1); // copy all values from Arraylist1 to anotherList
27+
System.out.println("Another list values are:" +anotherList);
28+
System.out.println("--------------------------------------------------");
29+
30+
anotherList.clear(); // clearing all values in anotherList
31+
System.out.println(anotherList);
32+
33+
arraylist1.remove(1); // remove the index value of 1 - TATA
34+
arraylist1.remove("BMW"); //remove the value from arraylist
35+
System.out.println(arraylist1);
36+
37+
arraylist1.add(null); //arrayList accepts null insertion
38+
System.out.println(arraylist1);
39+
40+
arraylist1.set(1, "SWIFT"); // use 'set' method to replace the value in the list
41+
System.out.println("The value replaced with swift in the list:" +arraylist1 );
42+
43+
44+
System.out.println(arraylist1.isEmpty()); // list is not empty hence false.
45+
46+
47+
// Iterate - Using Loops concept
48+
for (String value : arraylist1) { //using 'for each' loop
49+
System.out.println("using for each loop: "+value);
50+
}
51+
System.out.println("--------------------------------------------------");
52+
53+
for (int i=0; i<arraylist1.size(); i++) {
54+
System.out.println("Using for loop only: "+arraylist1.get(i));
55+
}
56+
System.out.println("--------------------------------------------------");
57+
58+
59+
//using List Iterator -- ListIterator<String> >>>> accepts forward and reverse direction allowed
60+
ListIterator<String> iterator1 = arraylist1.listIterator();
61+
while(iterator1.hasNext()) { // fetching values in move forward direction
62+
System.out.println(iterator1.next());
63+
}
64+
System.out.println("--------------------------------------------------");
65+
66+
while(iterator1.hasPrevious()) {
67+
System.out.println(iterator1.previous());
68+
}
69+
System.out.println("--------------------------------------------------");
70+
71+
// Iterator -- only forward direction allowed
72+
Iterator<String> iterator2 = arraylist1.iterator();
73+
while(iterator2.hasNext()) { // fetching values in move forward direction
74+
System.out.println(iterator2.next());
75+
}
76+
System.out.println("--------------------------------------------------");
77+
78+
79+
}
80+
81+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package learnList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ArrayListHeterogeneous {
7+
8+
public static void main(String[] args) {
9+
10+
List al = new ArrayList(); // if we skip the generics name <String>, it is heterogeneous type and it accepts all data types.
11+
al.add(10);
12+
al.add("name");
13+
System.out.println(al);
14+
15+
}
16+
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnList;
2+
3+
public class LinkedListExample {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package learnString;
2+
3+
public class StringExample {
4+
5+
public static void main(String[] args) {
6+
String name = "Ajeethkumar"; //string literal
7+
int num = 5;
8+
9+
// returns char value for the particular index
10+
System.out.println(name.charAt(1)); //o/p value is 'j'
11+
12+
// returns string length
13+
System.out.println(name.length());
14+
15+
//compare the input with the given string value
16+
System.out.println(name.equals("Ajeeth"));
17+
18+
//compare with input and ignore the case sensitive
19+
System.out.println(name.equalsIgnoreCase("AJEETH KUMAR"));
20+
21+
//check if string is empty or not
22+
System.out.println(name.isEmpty());
23+
24+
//returns true or false based on the input given is present in the string
25+
System.out.println(name.contains("j"));
26+
27+
// take a particular portion of the string using 'begin index' and 'end index'
28+
System.out.println(name.substring(2)); // the value starts with begin index value; o/p - 'eethkumar'
29+
System.out.println(name.substring(1, 5)); // o/p is 'jeet' - end index value is not added
30+
31+
//appends the value to the given string
32+
System.out.println(name.concat(" charan"));
33+
34+
//replace char(s) in the string
35+
System.out.println(name.replace("e", "z")); // char z is replaced for e in the name
36+
System.out.println(name.replace("kumar", "king")); //kumar is replaced with a new value
37+
38+
//find the position of a char in the given string
39+
System.out.println(name.indexOf("e")); //it will show the index value of first occurence of char 'e' = 2
40+
41+
//find the index position of a char after the specified char
42+
System.out.println(name.indexOf("k", 4)); //op = 6
43+
System.out.println(name.indexOf("t", 5)); // op -1
44+
System.err.println(name.indexOf("kum", 4));
45+
46+
// trim the string value
47+
System.out.println(name.trim());
48+
49+
//convert the given data type int to string
50+
System.out.println(String.valueOf(num)); // the int value is converted into String data type
51+
52+
//convert from uppercase to lowercase
53+
String uppercase = "AJEETH";
54+
System.out.println(uppercase.toLowerCase());
55+
56+
//convert from lowercase to uppercase
57+
String lowercase = "kumar";
58+
System.out.println(lowercase.toUpperCase());
59+
60+
//returns the output with a delimiter symbol - add delimiter in between the values
61+
System.out.println(String.join("|", "learn", "auto", "online")); //op is = learn|auto|online
62+
System.out.println(String.join("/", "14","08","2020")); //op is 14/08/2020
63+
System.out.println(String.join("&&", "aji", "kumar"));
64+
65+
//split the sentence by unique char
66+
String splitSent = "I,am, a, good,boy!";
67+
String[] splittedWords = splitSent.split(",");
68+
for (String eachWord : splittedWords) {
69+
System.out.println(eachWord);
70+
}
71+
72+
73+
}
74+
75+
}

JavaBasicPrograms/src/main/java/staticMethodTypes/LearnStaticMethodExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public static void staticMethod() {
99

1010

1111
public void nonStatic() {
12-
staticMethod(); // can call a static method to any static or non-static methods simply without creating object
12+
staticMethod(); // can call a static method from any static or non-static methods simply without creating object
1313
System.out.println("It is non-static method");
1414
}
1515

1616
public static void main(String[] args) {
17-
staticMethod(); //no need (not necessary) to create object for static methods
17+
staticMethod(); //no need (not necessary) to create object for static methods, simply call the method name
1818

1919
LearnStaticMethodExample stac = new LearnStaticMethodExample();
2020
stac.nonStatic(); // need object to call non-static method(s)

JavaBasicPrograms/src/main/java/staticMethodTypes/LearnStaticVariable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public static void main(String[] args) {
1313
// columns=20; // cannot call the non-static variable without object
1414

1515
LearnStaticVariable object1 = new LearnStaticVariable();
16-
object1.rows=100;
16+
object1.rows=100; // creating object for 'static' variable is NOT necessary
1717
object1.columns=25;
1818
object1.table="Table1";
19-
object1.name="Name1";
19+
name="Name1"; //defining value to the 'static' variable without using object
2020

2121
LearnStaticVariable object2 = new LearnStaticVariable();
22-
object2.rows=300;
22+
rows=300; //defining value to the static variable without object
2323
object2.columns = 50;
2424
object2.name="Name2";
2525
object2.table="Table2";

0 commit comments

Comments
 (0)