Java笔记

来源:互联网 发布:coremail知乎 编辑:程序博客网 时间:2024/04/28 07:41
 


1.

数组容量固定,不可扩充。容器则可以是动态增长,大小可扩充,容器内只能定义对象类型,collect(set、list、queue)和Map是容器的两类,注Set中对象不可重复,List中对象可重复,Map中对象是以一对出现的<1,"AAA">,1是界值(不能重复)AAA是数据。List下有一个ArrayList、LinkedList。

import java.util.*;

public class Test{

             public static void main(String[] args){

             ArrayList a = new ArrayList();

              a.add("AAA");

              a.add("BBB");

             System.println(a);

         }

}//存入容器内时类型丢失,默认为Object,取出时必须进行类型转换。

附:

            {

                   ArrayList<String>  a = new ArrayList<String>();

                            a.add("AAA");

                                      //存入容器内的都是字符串类型,取出时仍是字符串类型。

2.

范型程序设计:

public class Test<T>{

     void f(T t){  }

            Test<String> t = new Test<String>();  //创建字符串类型对象

 

}

 

}

 

3.

{

              LinkedList<String> l= new LinkedList<String>();

                    L.add("A");

                    L.add("B");

                    L.add("C");

                 System.out.println(L);

}

附:

L.addFirst("D");

System.out.println(L);

L.removeAll(L);

if(L.isEmpty())

    System.out.println("Empty");

 

4.

Iterator迭代器,遍历容器中的元素,hasNext()方法判断是否到最后。

          eg:

               Iterator it = L.iterator();  //定义it迭代器

               while(it.hasNext())

                    System.out.println(it.next());

eg:

class Stack<T>{

private LinkedList<T> s = new LinkedList<T>();

void push(T t)   //入栈

{

s.addFirst(t);

}

}

void pull(){

System.out.println(s.getFirst());

s.removeFirst();

}

boolean empty(){

return s.isEmpty();      //判断栈是否为空的方法。

}

 

import java.util.*;

public class Test{

public static void main(String []args){

Stack<String> s = new Stack<String>();

for(String s1:"My dog has fleas".split(" "))     //forreach 以空格分离字符串s1

s.push(s1);

while(!s.empty())

           s.pull();

}

}       //输出:fleas

                     has

                     dog

                     My

附:

Stack<Integer> s = new Stack<Integer>();

for(int i= 1;i<=5;i++)

          s.push(new Integer(i));

5.

HashSet 哈希:适合搜索,因输出无重复项。

HashSet<Integer> hs = new HashSet<Integer>();

Random rand = new Random(47);

for(int i = 0; i<10000; i++)

       hs.add(rand.nextInt(30));      //产生0~29的随机数

TreeSet功能与哈希一样,但结果排序。

 

6.

Map:HashMap、TreeMap

eg:

HashMap<Integer,String> m = new HashMap<Integer,String>();

      m.put(new Integer(1), "AAA");

      m.put(new Integer(2),"BBB");

      m.put(new Integer(2), "CCC");

      System.out.println(m);

//输出:[1 = AAA , 2 = CCC]  

界值重复,覆盖。

TreeMap用界值排序。

eg:

HashMap<Integer,Integer> m = new HashMap<Integer,Integer>();

Random rand = new Random(47);

for(int i = 0; i<10000; i++){

int r = rand.nextInt(20);

Integer freq = m.get(r); //检索是否已有

m.put(r,freq == null ? 1:freq+1);

 

}

System.out.println(m);

 

7.

import java.awt.*;

import javax.swing.*;

public class Test extends JApplet{

public void init(){

         {

           G g = new G();

           add(g);

                }

public static void main(String []args){

               JFrame f = new JFrame("画图");

               Test t = new Test();

               f.add(t);t.init();                    //小应用程序加载窗体必须手动调用。

               t.start();

               f.setSize(400,300);

               f.setVisible(true);

               f.setDefaultCloseOperation(f.EXIT_ON_CLOSE)

                 

                                                         }

}

class G extends Jpanel{

          public void paintComponent(Graphics g){

                                                      int x = getWidth();

                                                      int y = getHeight();

                                                      g.drawString("Hello", x/2,y/3);

                                                                              }

                                        }

}

 

附:

drawLine(int x1, int y1,  int x2, int y2) 画直线

Font f = new Font("黑体", Font.ITALIC.20);

g.setFont(f);

 

 

 

 

0 0
原创粉丝点击