Saturday, April 13, 2019

ROW ARRAY ????

public static void main(String[] args) {
 
String[][] schoolbagArray = new String[4][2];
schoolbagArray[0] = new String[] {"Books-0", "Notebooks-00", "Notebooks-000"};
schoolbagArray[1] = new String[] {"Books-1", "Notebooks-11", "Notebooks-111"};
schoolbagArray[2] = new String[] {"Books-2", "Notebooks-22", "Notebooks-222"};
schoolbagArray[3] = new String[] {"Books-3", "Notebooks-33", "Notebooks-333"};

System.out.print(schoolbagArray [0][0] + "  ");
System.out.print("  " + schoolbagArray [0][1] + " ");
System.out.print(schoolbagArray [0][2] + "  " + "\n");

System.out.print(schoolbagArray [1][0] + "  ");
System.out.print("  " + schoolbagArray [1][1] + " ");
System.out.print(schoolbagArray [1][2] + "\n");

System.out.print(schoolbagArray [2][0] + "  ");
System.out.print("  " + schoolbagArray [2][1] + " ");
System.out.print(schoolbagArray [2][2] + "\n");

System.out.print(schoolbagArray [3][0] + "  ");
System.out.print("  " + schoolbagArray [3][1] + " ");
System.out.print(schoolbagArray [3][2] + " ");

}

inside and outside main loop

public static void main(String[] args) {
/*
SupremeSuperClassC supremeSuperClassC = new SubClassB();
supremeSuperClassC.method3();
supremeSuperClassC.method4();
*/
String[][] schoolbagArray = new String[4][2];
  schoolbagArray[0] = new String[] {"Pens", "Pencils"};
  schoolbagArray[1] = new String[] {"Books", "Notebooks"};
  System.out.println( schoolbagArray [1][0] );
}
String[][] schoolbagArray = new String[4][2];
  schoolbagArray[0] = new String[] {"Pens", "Pencils"};
  schoolbagArray[1] = new String[] {"Books", "Notebooks"};
  System.out.println( "sssssssssssss");

Thursday, April 11, 2019

continue keyword example

import java.util.ArrayList;
import java.util.Iterator;

public class Continue {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
a1.add("1");a1.add("2");a1.add("3");
a1.add("4");a1.add("5");a1.add("6");
a1.add(null);a1.add("");a1.add(" ");

Iterator itr = a1.iterator();
while(itr.hasNext()) {
String a = (String) itr.next();
if(null==a || a.equals("")|| a.equals(" ")) {
continue;
}
else {
System.out.println("else loop " + a);
}
}
}
}

throws Exception class

public class ExceptionThrow {
public static void main(String[] args) throws Exception {
try {
throw new Exception("Exception Caught in the Exception Super class and not in ExceptonA catch block");
}
catch (ExceptionA e) {
System.out.println("e.getMessage() " + e.getMessage());
}
}
}
class ExceptionA extends ExceptionB {
String s;
public ExceptionA(String s) {
super(s);
}
}
class ExceptionB extends Exception{
public ExceptionB(String s) {
super(s);
}
}

Custom Exception Example

public class ExceptionThrow {
public static void main(String[] args) {
try {
throw new ExceptionA("EXCEPTION CAUGHT");
}
catch (ExceptionA e) {

System.out.println(e.getMessage());

}
}
}

(A)
class ExceptionA extends ExceptionB {
String s;
public ExceptionA(String s) {
//this.s=s;
super(s);
}
}
class ExceptionB extends Exception{

public ExceptionB(String s) {
super(s);
}
}
(B)
class ExceptionA extends ExceptionB {
String s;
public ExceptionA(String s) {
this.s=s;
        }
public String getMessage(){
return this.s;
}
}
class ExceptionB extends Exception{
}

Wednesday, April 10, 2019

Comparator before and after sorting

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Student {
int a;
String b,c;
public Student(int a, String b, String c) {
this.a = a;
this.b = b;
this.c = c;
}
public String toString() {
return this.a + "--"+ this.b + "--" + this.c + "--";
}
}
class Teacher implements Comparator<Student> {
@Override
public int compare(Student object1, Student object2) {
return object1.b.compareTo(object2.c);
}
}
class ComparatorBeforeandAfterSorting {
public static void main(String[] args) {
ArrayList<Student> a1 = new ArrayList<Student>();
a1.add(new Student(1,"A","Z"));
a1.add(new Student(1,"F","M"));
a1.add(new Student(1,"R","Q"));
a1.add(new Student(1,"E","U"));
a1.add(new Student(1,"C","U"));
System.out.println("before sorting");
for(int i=0;i<a1.size();i++) {
System.out.println(a1.get(i));
}
Collections.sort(a1, new Teacher());
System.out.println("after sorting");
for (int i = 0; i < a1.size(); i++) {
System.out.println(a1.get(i));
}
}
}

Sunday, April 7, 2019

StringInsideStringBuffer Example

import java.util.HashMap;
public class StringInsideStringBuffer {
public static void main(String[] args) {
HashMap<String,String> hmap = new HashMap();
hmap.put("appa-1", "amma-1");
StringBuffer sb1 = new StringBuffer(new String("balaji"));
System.out.println(sb1.toString());
StringBuffer sb2 = new StringBuffer(hmap.get("appa-1"));
System.out.println(sb2.toString());
}
}