温馨提示:和谐,温馨,快乐的网上家园(*^__^*) 嘻嘻……

虾米论坛's Archiver

落地 发表于 2011-1-20 09:28

[11/01/20] java 私塾第五章笔记整理

java 私塾第五章笔记整理

数组是由相同类型的若干项数据组成的一个数据集合,也就是说数组是用来集合相同类型的对象并通过一个名称来引用这个对象。
可以声明任何类型的数组-----原始类型或类类型。
当数组声明的方括号在左边时,该方括号可以用于所有位于其右边的变量:char [] s,y。
数组一旦被创建,在内存里占用连续的内存地址。
[color=red]数组的静态性:数组一旦被创建,就不能更改数组的长度。[/color]

        Point [] p = new Point[3];
        new int[][4]是非法的。

数组的复制
        System.arraycopy(myArray,0,hold,0,myArray.length);
数组排序
        Arrays.sort(a);

定义一个一维的int数组,先创建它,并初始化它,给它赋值,然后输出其中的一个值[code]public class Arr{
     public static void main(String args[]){
           int a[] = new int[5];
        a[0]=1;
           a[1]=2;
           System.out.println(a[0]);
     }
  }[/code]定义一个一维的A类型数组,直接定义并赋值,然后输出其中的一个值[code]
public class A{
     public static int i;
     public static void main(String args[]){
        A aa = new A();
        A bb = new A();
        A a[] = {aa,bb};
        a[0].i=2;
            System.out.println(a[0]);
     }
}[/code]把上面的数组改成2维的数组[code]
public class A{
    public static int i;
    public static void main(String args[]){
        A a[][] = new A[5][5];
        a[0][0].i=2;
        System.out.println(a[0][0]);
    }
}[/code]举例说明数组拷贝方法的使用:arraycopy方法,[code]
public class A{
    public static void main(String args[]){
        int a[] = new int[5];
        int b[] = new int[5];
        System.arraycopy(a[5],0,b[5],0,a.length);
        System.out.println(b[0]);
    }
}[/code]常见的排序算法[code]
public class SortAll {   
    public static void main(String[] args) {   
        int[] i = { 1, 5, 6, 12, 4, 9, 3, 23, 39, 403, 596, 87 };   
        System.out.println("----冒泡排序的结果:");   
        maoPao(i);   
        System.out.println();   
        System.out.println("----选择排序的结果:");   
        xuanZe(i);   
        System.out.println();   
        System.out.println("----插入排序的结果:");   
        chaRu(i);   
    }
  
    // 冒泡排序   
    public static void maoPao(int[] x) {   
        for (int i = 0; i < x.length; i++) {   
            for (int j = i + 1; j < x.length; j++) {   
                if (x[i] > x[j]) {   
                    int temp = x[i];   
                    x[i] = x[j];   
                    x[j] = temp;   
                }   
            }   
        }   
        for (int i : x) {   
            System.out.print(i + " ");   
        }   
    }   
    // 选择排序
    public static void xuanZe(int[] x) {   
        for (int i = 0; i < x.length; i++) {   
            int lowerIndex = i;   
            // 找出最小的一个索引   
            for (int j = i + 1; j < x.length; j++) {   
                if (x[j] < x[lowerIndex]) {   
                    lowerIndex = j;   
                }   
            }   
            // 交换
            int temp = x[i];   
            x[i] = x[lowerIndex];   
            x[lowerIndex] = temp;   
        }   
        for (int i : x) {   
            System.out.print(i + " ");   
        }   
    }  

    // 插入排序   
    public static void chaRu(int[] x) {   
        for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦   
            for (int j = i; j > 0; j--) {   
                if (x[j] < x[j - 1]) {   
                    int temp = x[j];   
                    x[j] = x[j - 1];   
                    x[j - 1] = temp;   
                }   
            }   
        }   
        for (int i : x) {   
            System.out.print(i + " ");   
        }   
    }   
}[/code]二分法查找[code]
package com.sghlwxf.binarySoft;

public class TestBinarySoft{
   public static void main(String args[]){
      int[] array= new int[]{4, 12, 23, 33, 45, 53, 65, 78, 88, 90 };
      //定义要查找的数
      int seek = 76;
      //定义下标
      int index = 0;
      //定义起始位置
      int start = 0;
      //定义结束位置
      int end = 0;
      //定义计数器
      while(true){
         count ++;
         //int index = (start + end)/2;
         //为了防止start+end溢出,所以写成start+((end - start)/2)
         int index =  start + ((end - start)/2);
         if(array[index] <seek){
            start = index;
         }else if(array[index]>seek){
            end = index;
         }else{
            break;
         }
     }
     System.out.pritln("所运行的次数是"+count+"地址为"+index);
   }
}[/code]【此处有二叉树图片,可以到java 私塾官网下载完整笔记:w ww.javass.cn】[code]
/** 二叉树节点 */
public class BTNode {
    private char key;
    private BTNode left, right;
    public BTNode(char key) {
        this(key, null, null);
    }
    public BTNode(char key, BTNode left, BTNode right) {
        this.key = key;
        this.left = left;
        this.right = right;
    }
    public char getKey() {
        return key;
    }
    public void setKey(char key) {
        this.key = key;
    }
    public BTNode getLeft() {
        return left;
    }
    public void setLeft(BTNode left) {
        this.left = left;
    }
    public BTNode getRight() {
        return right;
    }
    public void setRight(BTNode right) {
        this.right = right;
    }
}[/code][code]/** 二叉树遍历 */
public class BinTree {
    protected BTNode root;
    public BinTree(BTNode root) {
        this.root = root;
    }
    public BTNode getRoot() {
        return root;
    }
    /** 构造树 */
    public static BTNode init() {
        BTNode a = new BTNode('A');
        BTNode b = new BTNode('B', null, a);
        BTNode c = new BTNode('C');
        BTNode d = new BTNode('D', b, c);
        BTNode e = new BTNode('E');
        BTNode f = new BTNode('F', e, null);
        BTNode g = new BTNode('G', null, f);
        BTNode h = new BTNode('H', d, g);
        return h;// root
    }
    /** 访问节点 */
    public static void visit(BTNode p) {
        System.out.print(p.getKey() + " ");
    }
    /** 递归实现前序遍历 */
    protected static void preorder(BTNode p) {
        if (p != null) {
            visit(p);
            preorder(p.getLeft());
            preorder(p.getRight());
        }
    }
    /** 递归实现中序遍历 */
    protected static void inorder(BTNode p) {
        if (p != null) {
            inorder(p.getLeft());
            visit(p);
            inorder(p.getRight());
        }
    }
    /** 递归实现后序遍历 */
    protected static void postorder(BTNode p) {
        if (p != null) {
            postorder(p.getLeft());
            postorder(p.getRight());
            visit(p);
        }
    }
    /** 非递归实现前序遍历 */
    protected static void iterativePreorder(BTNode p) {
        Stack<BTNode> stack = new Stack<BTNode>();
        if (p != null) {
            stack.push(p);
            while (!stack.empty()) {
                p = stack.pop();
                visit(p);
                if (p.getRight() != null)
                    stack.push(p.getRight());
                if (p.getLeft() != null)
                    stack.push(p.getLeft());
            }
        }
    }
    /** 非递归实现后序遍历 */
    protected static void iterativePostorder(BTNode p) {
        BTNode q = p;
        Stack<BTNode> stack = new Stack<BTNode>();
        while (p != null) {
            // 左子树入栈
            for (; p.getLeft() != null; p = p.getLeft())
                stack.push(p);
            // 当前节点无右子或右子已经输出
            while (p != null && (p.getRight() == null || p.getRight() == q)) {
                visit(p);
                q = p;// 记录上一个已输出节点
                if (stack.empty())
                    return;
                p = stack.pop();
            }
            // 处理右子
            stack.push(p);
            p = p.getRight();
        }
    }
    /** 非递归实现中序遍历 */
    protected static void iterativeInorder(BTNode p) {
        Stack<BTNode> stack = new Stack<BTNode>();
        while (p != null) {
            while (p != null) {
                if (p.getRight() != null)
                    stack.push(p.getRight());// 当前节点右子入栈
                stack.push(p);// 当前节点入栈
                p = p.getLeft();
            }
            p = stack.pop();
            while (!stack.empty() && p.getRight() == null) {
                visit(p);
                p = stack.pop();
            }
            visit(p);
            if (!stack.empty())
                p = stack.pop();
            else
                p = null;
        }
    }
    public static void main(String[] args) {
        BinTree tree = new BinTree(init());
        System.out.print(" Pre-Order:");
        preorder(tree.getRoot());
        System.out.println();
        System.out.print("  In-Order:");
        inorder(tree.getRoot());
        System.out.println();
        System.out.print("Post-Order:");
        postorder(tree.getRoot());
        System.out.println();
        System.out.print(" Pre-Order:");
        iterativePreorder(tree.getRoot());
        System.out.println();
        System.out.print("  In-Order:");
        iterativeInorder(tree.getRoot());
        System.out.println();
        System.out.print("Post-Order:");
        iterativePostorder(tree.getRoot());
        System.out.println();
    }
}[/code]输出结果
Pre-Order:H D B A C G F E
  In-Order:B A D C H G E F
Post-Order:A B C D E F G H
Pre-Order:H D B A C G F E
  In-Order:B A D C H G E F
Post-Order:A B C D E F G H

小巫 发表于 2011-5-29 17:53

好久没去关注了,今天再去看看,谢谢了!

木束负 发表于 2011-7-20 02:33

百度一下,找到相关网页约15,600篇,用时0.001秒     

test123 发表于 2011-7-20 02:41

我刚来~~~嘿嘿~~

秘方 发表于 2011-7-21 02:49

偶的天啊!疯了 这年头盗版还真多~~~~~~

梵音 发表于 2011-7-22 02:38

呵呵 放心 如我算错 你以后来砸我的帖子 呵呵

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.