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());
}
}

java.util.NoSuchElementException Example

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorForLoop {
public static void main(String[] args) {
List l1 = new ArrayList();
l1.add("1");l1.add("2");l1.add("3");
l1.add("4");l1.add("5");l1.add("6");
for(Iterator itr=l1.iterator();itr.hasNext();itr.next()) {
while(itr.hasNext()) {
System.out.print(itr.next());
}
}
}
}

Answer : 

1 2 3 4 5 6
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(ArrayList.java:862)

at IteratorForLoop.main(IteratorForLoop.java:10)

Reason :

because itr.next() has been given in the for Loop

Iterator for loop


ConcreteClass extending AbstractSuperClass extending AnotherAbstractSuperClass extending... Example

public abstract class SupremeSuperClassA {
abstract void methodA();
abstract void methodB();
}

public abstract class SuperClassA extends SupremeSuperClassA{
abstract void method1();
abstract void method2();
abstract void method3();
}

public abstract class SubClassB extends SuperClassA {
@Override
void method1() {
}
}

public class SubClassC extends SubClassB {
@Override
void method3() {
}
@Override
void method2() {
}
@Override
void methodA() {
}
@Override
void methodB() {
}
}

Math.pow example

package Math;
public class Math_pow {
public static void main(String[] args) {

int val;
val = (int) Math.pow(2, 5);
System.out.println(val);

}
}

CollectionSizetoStringArray Example

import java.util.List;
import java.util.ArrayList;
public class CollectionSizetoStringArray {
public static void main(String[] args) {
List l1 = new ArrayList();
l1.add("10");l1.add("20");l1.add("30");l1.add("40");
String[] array = new String[5];
array = (String[]) l1.toArray(array);
for(String arrayElement : array) {
System.out.println("arrayElement-1 " + arrayElement);
}
array = (String[]) l1.toArray(new String[5]);
array[4] = "Balaji Balaji Balaji";
for(String arrayElement : array) {
System.out.println("arrayElement-2 " + arrayElement);
}

  String[] arraySizeX = (String[]) l1.toArray(new String[l1.size()+100]);
  for(String arrayElement : arraySizeX) {
        System.out.println("arrayElement-1 " + arrayElement);
  }
}
}

Saturday, April 6, 2019

radix example

public class IntegerparseIntExample {
public static void main(String[] args) {
int tenA = Integer.parseInt("1111", 2);
System.out.println(tenA);

  int a=2;
  int tenB = Integer.parseInt("1111", ++a);
  System.out.println(tenB);

}
}

NumberFormatException example

public class IntegerparseIntExample {
public static void main(String[] args) {
int ten = Integer.parseInt("Not a number", 2);
System.out.println(ten);
}
}

Answer:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Not a number"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)

at IntegerparseIntExample.main(IntegerparseIntExample.java:5)

StringArrays via Collection Example

import java.util.*;
public class StringArraysVIACollection_Example {
public static void main(String[] args) {
String returnArray[] = method1();
System.out.println(returnArray[0]);
System.out.println(returnArray[1]);
System.out.println(returnArray[2]);
System.out.println(returnArray[3]);
System.out.println(returnArray[4]);
System.out.println(returnArray[5]);
System.out.println(returnArray.length);
}
public static String[] method1() {
List l1 = new ArrayList();
l1.add("10");l1.add("20");l1.add("30");l1.add("40");l1.add("50");
String[] array = new String[6];
int i = 0;
Iterator itr = l1.iterator();
while(itr.hasNext()) {
array[i++] = (String) itr.next();
}
return array;
}
}

Thursday, April 4, 2019

adding method in an enum class

import java.io.Serializable;

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
   public static EnumValues fromString(String bang_and_jang_kutties) {
       return BANG_AND_JANG_KUTTIES;
   }
   public static EnumValues getvalues() {
       return BANG_AND_JANG_KUTTIES;
   }
   public static void main(String[] args) {
   EnumValues[] array = EnumValues.values();
   System.out.println(array[0] +" "+ array[1]);
   System.out.println(EnumValues.getvalues());
   System.out.println(EnumValues.fromString("The Best"));
   }
}

enum values()

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
 
   public EnumValues getValues(String input) {
   return BALAJI;
   }
   public static void main(String[] args) {
   System.out.println(EnumValues.values());  
   EnumValues[] array = EnumValues.values();
   System.out.println(array[0] +" "+ array[1]);
   }
}

Wednesday, April 3, 2019

How to add Initial Capacity to ArrayList

We can add initial capacity to arraylist like this

List list = new ArrayList(new String[10].length + 5);

Class Array Example


(a)
public class MainArrayTest {
public static void main(String[] args) {
MyExampleTest[] myexamples = new MyExampleTest[20];
myexamples[0] = new MyExampleTest();
myexamples[0].setQueryKey1(EnumValues.BALAJI);
myexamples[0].setQueryKey2(EnumValues.BANG_AND_JANG_KUTTIES);
System.out.println(myexamples[0].getQueryKey1());
System.out.println(myexamples[0].getQueryKey2());

myexamples[1] = new MyExampleTest();
myexamples[1].setQueryKey1(EnumValues.BALAJI);
myexamples[1].setQueryKey2(EnumValues.BANG_AND_JANG_KUTTIES);
System.out.println(myexamples[1].getQueryKey1());
System.out.println(myexamples[1].getQueryKey2());

myexamples[2] = new MyExampleTest();
myexamples[2].setServiceType3("BALAJI");
myexamples[2].setServiceType4("IS THE GREATEST");
System.out.println(myexamples[2].getServiceType3());
System.out.println(myexamples[2].getServiceType4());

myexamples[3] = new MyExampleTest();
String[] familyArraysKuttiValues = { "Bang","Jang","Chellam","Kutties" };
myexamples[3].setServiceType5(new String[10]);
myexamples[3].setServiceType6(familyArraysKuttiValues);
String kutties = myexamples[3].getServiceType6()[0] + " and " +
myexamples[3].getServiceType6()[1] + " and " +
myexamples[3].getServiceType6()[2] + " and " +
myexamples[3].getServiceType6()[3];
System.out.println(kutties + " are the best in the world");

}
}

(b)
public class MyExampleTest {

private EnumValues serviceType1;
private EnumValues serviceType2;

public void setQueryKey1(EnumValues serviceType1) {
this.serviceType1 = serviceType1;
}
public void setQueryKey2(EnumValues serviceType2) {
this.serviceType2 = serviceType2;
}
public EnumValues getQueryKey1() {
return serviceType1;
}
public EnumValues getQueryKey2() {
return serviceType2;
}

private String serviceType3;
private String serviceType4;

public String getServiceType3() {
return serviceType3;
}
public void setServiceType3(String serviceType3) {
this.serviceType3 = serviceType3;
}
public String getServiceType4() {
return serviceType4;
}
public void setServiceType4(String serviceType4) {
this.serviceType4 = serviceType4;
}

private String serviceType5[];
private String serviceType6[];

public String[] getServiceType5() {
return serviceType5;
}
public void setServiceType5(String[] serviceType5) {
this.serviceType5 = serviceType5;
}
public String[] getServiceType6() {
return serviceType6;
}
public void setServiceType6(String[] serviceType6) {
this.serviceType6 = serviceType6;
}
}

(c)

import java.io.Serializable;

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
}

BooleanTest Example

package searchOrange;
import java.util.HashMap;
public class BooleanTest {
public static void main(String[] args) {
boolean value1=false;
HashMap hmap1 = new HashMap();
hmap1.put("balaji-1",value1?Boolean.TRUE:Boolean.FALSE);
System.out.println("balaji-1 " + hmap1.get("balaji-1"));
//if the boolean value is false
//its taking in only the second value

hmap1.put("balaji-2",value1?Boolean.FALSE:Boolean.TRUE);
System.out.println("balaji-2 " + hmap1.get("balaji-2"));
//if the boolean value is false
//its taking in only the second value


boolean value2=true;
HashMap hmap2 = new HashMap();
hmap2.put("balaji-3",value2?Boolean.FALSE:Boolean.TRUE);
System.out.println("balaji-3 " + hmap2.get("balaji-3"));
//if the boolean value is true
//its taking in only the first value 

hmap2.put("balaji-4",value2?Boolean.TRUE:Boolean.FALSE);
System.out.println("balaji-4 " + hmap2.get("balaji-4"));
//if the boolean value is true   
//its taking in only the first value
}
}

Answer : 

balaji-1 false
balaji-2 true
balaji-3 false
balaji-4 true

BECAUSE THE VALUE OF BOOLEAN IS CHECKED FIRST AS TRUE AND THEN AS FALSE IN THE BOOLEAN CONDITION ITSELF 

AS IN ANYCASE WITH ANY IF ELSE CONDITION...