eboy's java

交流QQ: 84461376
posts - 3, comments - 0, trackbacks - 0, articles - 0

2012年7月22日

package com.eboy.androidsdreadwrite;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.os.Bundle;
import android.os.Environment;

import android.app.Activity;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    Button btn1 = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);
        
        btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                File file = new File(Environment.getExternalStorageDirectory(), "eboy.txt");
                
                OutputStream os = null;
                
                try {
                    os = new FileOutputStream(file);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                try {
                    os.write("Hello eboy, Hello Frr.".getBytes());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                
                try {
                    os.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }    
                Toast toast = Toast.makeText(MainActivity.this, "写文件到SD卡成功。eboy.txt", Toast.LENGTH_SHORT); 
                toast.show();
            }
        });    
        
        
        
        
           
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu); 
        
        
         
        
        
        return true;
    }

    
}

posted @ 2012-07-22 01:39 eboy 阅读(1618) | 评论 (0)编辑 收藏

2012年7月19日

/**
 * 在使用泛型时,如果两边同时用到了泛型,两边必须一致,例:ArrayList<String> list = new ArrayList<String>();
 * 在使用泛型时,如果仅一边用到了泛型,这是可以的,为了保持向后兼容性,例: ArrayList<String> list = new ArrayList();  ArrayList list = new ArrayList<String>();
 
*/

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

public class generic {

    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();        
    
        list1.add("bbb");
        list1.add("cc");
        list1.add("ddd");
        list1.add("eeee");
        list1.add("ffffd");
        
        //方式1
        Iterator<String> Strs = list1.iterator();
        while (Strs.hasNext()){
            System.out.println(Strs.next());
        }        
        
        //方式2
        for (String s : list1){
            System.out.println(s);
        }
        
        
        Set<Integer> set = new HashSet<Integer>();        
        set.add(1);
        set.add(2);
        set.add(3);
        set.add(4);
        set.add(5);
        set.add(8);
        set.add(7);
        
        //方式1
        for (Integer i : set){
            System.out.println(i);
        }
        
        Map<Integer, String> map = new HashMap<Integer, String>();
        
        map.put(1, "eboy");
        map.put(2, "FRR");
        map.put(3, "LJ");
        //方式1
        Set<Integer> set1 = map.keySet();        
        Iterator<Integer> it = set1.iterator();            
        while (it.hasNext()){
            int key = it.next();            
            System.out.println(map.get(key));
        }
        //方式2
        Set<Map.Entry<Integer, String>> set2 = map.entrySet();
        
        Iterator<Map.Entry<Integer, String>> it2 = set2.iterator();
        while (it2.hasNext()){
            Map.Entry<Integer, String> entry = it2.next();
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
        //方式3 1.5
        for (Map.Entry<Integer, String> entry3 : map.entrySet()){
            System.out.println(entry3.getKey() + ": " + entry3.getValue());
        }    
        
        
        String arr[] = {"a", "b", "c", "d"};
        System.out.println(arr[0]);
        reverse(arr);
        System.out.println(arr[0]);
        
        
    }
    
    //加个@Text,可用JUnit单独测试test2方法 
    @Test
    public void test2(){
        int i;
        i = 15;
        String s;
        s = "asfdsaf";
        
        System.out.println("test" + i + s);
    }
    
    //自定义泛型:方法是泛型
    public static <T> void Test(T t){
        
    }
    
    //自定义泛型:返回值是泛型
    public static <T, E> T Test1(T t, E e){
        
        return null;
    }


    //泛型应用实例:交换数组元素
    static <T> void swap(T arr[], int pos1, int pos2){
        T temp = arr[pos1];
        arr[pos1] = arr[pos2];
        arr[pos2] = temp;
    }
    
     //泛型应用实例:接收一个任意数组,并颠倒数组中的所有元素
    static <T> void reverse(T arr[]){
        int pos1 = 0;
        int pos2 = arr.length - 1;        
        
        T temp = null;
        
        while (pos1 <= pos2){
            temp = arr[pos1];
            arr[pos1] = arr[pos2];
            arr[pos2] = temp;
            pos1++;
            pos2--;
        }
        
    }

}

posted @ 2012-07-19 22:51 eboy 阅读(143) | 评论 (0)编辑 收藏

2012年7月17日

//JAVA GUI 鼠标单击演示
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseKey {        
    public static void main(String[] args) {
        Frame f = new Frame("鼠标单击事件演示");
        f.setBounds(400, 400, 640, 480);
        f.setLayout(new FlowLayout());            
        Button btn = new Button("关闭");        
        f.add(btn);        
        btn.addMouseListener(new MouseAdapter() {    
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }            
        });                
        f.setVisible(true);    
    }
}

posted @ 2012-07-17 23:45 eboy 阅读(121) | 评论 (0)编辑 收藏