java 私塾第五章笔记整理
java 私塾第五章笔记整理数组是由相同类型的若干项数据组成的一个数据集合,也就是说数组是用来集合相同类型的对象并通过一个名称来引用这个对象。
可以声明任何类型的数组-----原始类型或类类型。
当数组声明的方括号在左边时,该方括号可以用于所有位于其右边的变量:char [] s,y。
数组一旦被创建,在内存里占用连续的内存地址。
数组的静态性:数组一旦被创建,就不能更改数组的长度。
Point [] p = new Point;
new int[]是非法的。
数组的复制
System.arraycopy(myArray,0,hold,0,myArray.length);
数组排序
Arrays.sort(a);
定义一个一维的int数组,先创建它,并初始化它,给它赋值,然后输出其中的一个值public class Arr{
public static void main(String args[]){
int a[] = new int;
a=1;
a=2;
System.out.println(a);
}
}定义一个一维的A类型数组,直接定义并赋值,然后输出其中的一个值
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.i=2;
System.out.println(a);
}
}把上面的数组改成2维的数组
public class A{
public static int i;
public static void main(String args[]){
A a[][] = new A;
a.i=2;
System.out.println(a);
}
}举例说明数组拷贝方法的使用:arraycopy方法,
public class A{
public static void main(String args[]){
int a[] = new int;
int b[] = new int;
System.arraycopy(a,0,b,0,a.length);
System.out.println(b);
}
}常见的排序算法
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 > x) {
int temp = x;
x = x;
x = 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 < x) {
lowerIndex = j;
}
}
// 交换
int temp = x;
x = x;
x = 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 < x) {
int temp = x;
x = x;
x = temp;
}
}
}
for (int i : x) {
System.out.print(i + " ");
}
}
}二分法查找
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 <seek){
start = index;
}else if(array>seek){
end = index;
}else{
break;
}
}
System.out.pritln("所运行的次数是"+count+"地址为"+index);
}
}【此处有二叉树图片,可以到java 私塾官网下载完整笔记:w ww.javass.cn】
/** 二叉树节点 */
public class BTNode {
∵ char key;
∵ 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;
}
}/** 二叉树遍历 */
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();
}
}输出结果
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
页:
[1]