Wednesday, October 29, 2014
Use BufferedReader to read a file
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class BufferedReaderTest {
public static void main(String[] args) throws Exception{
String fileName = "c:\\tmp\\test.txt";
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = "";
while ((line = br.readLine())!=null){
System.out.println("line="+line);
}
br.close();
fr.close();
}
}
Tuesday, October 28, 2014
Use BufferedWriter write to a file
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class BufferedWriterTest {
public static void main(String[] args) throws IOException{
String fileName = "C:\\tmp\\test.txt";
File file = new File(fileName); // File needs fileName
FileWriter fw = new FileWriter(file); // so here use Writer implementing class - FileWriter
BufferedWriter bw = new BufferedWriter(fw); //Writer can not be initialized b/c it's abstract class
try {
bw.write("this is my test file");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
bw.close(); // when close, the content can be written to the file
fw.close();
}
}
}
Sunday, October 26, 2014
Collections.synchronizedMap when map is used in thread
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
// HashMap is not synchronized
public class MapTest {
public static void main(String[] args){
Map<String, String> map = Collections.synchronizedMap(new HashMap<String, String>());
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
for (Map.Entry<String, String> entry:map.entrySet()){
System.out.println("key="+entry.getKey()+" value="+entry.getValue());
}
}
}
OUTPUT:
key=3 value=c
key=2 value=b
key=1 value=a
Sunday, October 12, 2014
Reverse Number
// how to write program:
// give an example
// identify number by variable
// convert to program
// verify program by example
// if number to reverse is 123
// 12 3 0x10+3
// 1 2 3x10+2
// 0 1 32x10+1
public class ReverseNumberTest {
public int reverseNumber(int number){
int reverse = 0;
while (number!=0){
reverse = reverse*10 + number % 10;
number = number / 10;
}
return reverse;
}
public static void main(String[] args){
ReverseNumberTest rnt = new ReverseNumberTest();
System.out.println("reverse number="+rnt.reverseNumber(123));
}
}
Thursday, October 9, 2014
Reverse String (recursive)
public class ReverseStringTest {
public String reverseString(String str){
// str.substring(0,2) returns index of 0 and 1 of str
return (str.length()==1? str: str.charAt(str.length()-1) + reverseString(str.substring(0,str.length()-1)));
}
public static void main(String[] args){
ReverseStringTest rst = new ReverseStringTest();
System.out.println(rst.reverseString("abcd"));
}
}
OUTPUT:
dcba
Tuesday, October 7, 2014
ArrayList Loop (for, Iterator, ListIterator)
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
// Iterator vs ListIterator
// 1. Iterator traverse List and Set; ListIterator traverse List
// 2. Iterator can go next; ListIterator can go next and previous
// 3. Iterator can not get Index; ListIterator can get Index
// 4. Iterator can NOT add item when traverse; ListIterator can add when traverse
// 5. Iterator can NOT replace item when traverse; ListIterator can replace when traverse
public class ArrayListTest {
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
// use enhanced for to loop
for (String str : list){
System.out.println(str);
}
// use Iterator to loop
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
// use ListIterator to loop
ListIterator li = list.listIterator();
while (li.hasNext()){
System.out.println(li.next());
}
}
}
OUTPUT:
A
B
C
A
B
C
A
B
C
Sunday, October 5, 2014
java.lang.Math.random() and java.util.Random
import java.util.Random;
public class RandomTest {
public static void main(String[] args){
// 1) java.util.Random to generate random number
Random random = new Random(1000); // 1000 is seed
for (int i = 0; i<3; i++){
System.out.println(random.nextInt()); // generates same random number because of seed is fixed
}
// 2) java.lang.Math.random() to generate random number
for (int i=0; i<3; i++){
System.out.println(Math.random()); // random double between 0 and 1, 0 is inclusive
}
}
}
OUTPUT:
-1244746321 (same)
1060493871 (same)
-1826063944 (same)
0.22284876712156954 (different)
0.19407514115469882 (different)
0.2939945031187672 (different)
Thursday, October 2, 2014
Loop Map using Map.Entry in map.entrySet()
import java.util.HashMap;
import java.util.Map;
public class MapEntryTest {
public static void main(String[] args){
Map<String, String > map = new HashMap<String, String>();
//initialize map
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
// entry in entrySet
for (Map.Entry<String, String> entry : map.entrySet()){
System.out.println("key="+entry.getKey()+" value="+entry.getValue());
}
}
}
OUTPUT:
key=3 value=c
key=2 value=b
key=1 value=a
HashMap
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class FindDuplicateInString {
public void findDuplicateInString(String str){
char[] chars = str.toCharArray();
Map<Character,Integer> map = new HashMap<Character,Integer>();
// constructs map
for (char ch : chars){
if (map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
}else{
map.put(ch,1);
}
}
// print out duplicate char
Set<Character> keys = map.keySet();
for (Character key : keys){
if (map.get(key)>1){
System.out.println("duplicate char: "+key);
}
}
}
public static void main(String[] args){
FindDuplicateInString duplicate = new FindDuplicateInString();
duplicate.findDuplicateInString("java");
}
}
OUTPUT:
duplicate char: a
ListIterator
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIteratorTest {
public static void main(String[] args){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
ListIterator<Integer> li = list.listIterator();
while (li.hasNext()){
System.out.println(li.next());
}
while (li.hasPrevious()){
System.out.println(li.previous());
}
}
}
OUTPUT:
1
2
3
3
2
1
Subscribe to:
Posts (Atom)