Monday, October 27, 2014

ITERATE JAVA

Iterate through elements Java Vector using ListIterator
import java.util.Vector;
import java.util.ListIterator; 
public class IterateThroughVectorUsingListIteratorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
ListIterator itr = v.listIterator();
System.out.println("Iterating through Vector elements in forward direction...");
while(itr.hasNext())
System.out.println(itr.next());
System.out.println("Iterating through Vector elements in backward direction...");
while(itr.hasPrevious())
System.out.println(itr.previous());
  }
}
Remove All Items From a List
import java.applet.Applet;
import java.awt.List;
public class RemoveAllItemsFromAList extends Applet
{
public void init()
{
List list = new List(5, true);
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
list.add("Six");
list.add("Seven");
add(list);
list.removeAll();
  }
}
Swap elements of Java ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class SwapElementsOfArrayListExample
{
 public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println("Before swaping, ArrayList contains : " arrayList);
Collections.swap(arrayList,0,4);
System.out.println("After swaping, ArrayList contains : " + arrayList);
   }
}
Get Foreground Color Of an Applet Window
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class GetForgroundColorExample extends Applet
{
public void paint(Graphics g)
{
  Color c = getForeground();
  g.drawString(c.toString(),50,50);
   }
}



Replace an element at specified index of Java ArrayList
import java.util.ArrayList;
public class ReplaceElementAtSpecifiedIndexArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.set(1,"REPLACED ELEMENT");
System.out.println("ArrayList contains...");
for(int index=0; index < arrayList.size(); index++)
System.out.println(arrayList.get(index));
   }
}
Replace an element at specified index of Java ArrayList
import java.util.ArrayList;
public class ReplaceElementAtSpecifiedIndexArrayListExample
{
public static void main(String[] args)
 {
ArrayList arrayList = new ArrayList();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.set(1,"REPLACED ELEMENT");
System.out.println("ArrayList contains...");
for(int index=0; index < arrayList.size(); index++)
System.out.println(arrayList.get(index));
   }
}



Create Custom Color Using HSB
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class CreateCustomColorUsingHSB extends Applet
public void paint(Graphics g)
 {
Color customColor = Color.getHSBColor(0.9f,0.5f,0.8f);
this.setForeground(customColor);
g.drawString("This is a custom HSB color", 10, 50);
  }
}
Convert short primitive to Short object
public class shortToShortExample
 {
public static void main(String[] args)
{
short s = 10;
Short sObj = new Short(s);
System.out.println(sObj);
   }
}
Read short from file using DataInputStream
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadShortFromFile
{
public static void main(String[] args)
{
 String strFilePath = "C://FileIO//readShort.txt";
   
try
{
FileInputStream fin = new FileInputStream(strFilePath);
DataInputStream din = new DataInputStream(fin);
short s = din.readShort();
System.out.println("short : " + s);
din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
      }
   }
}
Java String isEmpty
public class JavaStringIsEmptyExample
{
public static void main(String args[])
{
String str1 = "";
String str2 = null;
String str3 = "Hello World";
System.out.println("Is String 1 empty? :" + str1.isEmpty()); System.out.println("Is String 3 empty? :" + str3.isEmpty());
   }
}
Java float
import java.util.*;
public class JavaFloatExample
 {
public static void main(String[] args)
{
float = ;
float f = 10.4f;
System.out.println("Value of float variable f is :" + f);                
   }
}
Compare Two Numbers
public class CompareTwoNumbers
{
public static void main(String[] args)
{
int num1 = 324;
int num2 = 234;               
if(num1 > num2)
{
System.out.println(num1 + " is greater than " + num2);
}
else if(num1 < num2)
{
System.out.println(num1 + " is less than " + num2);
}
else
{
System.out.println(num1 + " is equal to " + num2);
      }
   }
}
Determine If Year Is Leap Year
public class DetermineLeapYearExample
{
 public static void main(String[] args)
{
 int year = 2004;                
 if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
System.out.println("Year " + year + " is a leap year");
else
System.out.println("Year " + year + " is not a leap year");
  }
}
   
If Else statement
public class SimpleIfElseStatementExample
 {
public static void main(String[] args)
{
int i = 0;       
if(i == 0)
System.out.println("i is 0");
else
System.out.println("i is not 0");
    }
}
If Else If statement
public class IfElseIfElseExample
{
public static void main(String[] args)
{
int i = 10;
if(i > 100)
System.out.println("i is grater than 100");
else if(i > 50)
System.out.println("i is grater than 50");
else
System.out.println("i is less than 50");
   }
}



If statement
public class SimpleIfStatementExample
{
           public static void main(String[] args)
{
  boolean blnStatus = true;
  if(blnStatus)
         System.out.println("Status is true");
    }

  }

No comments:

Post a Comment