for(int i=0;i System.out.println(“list["+i+"]=”+list.get(i)); } } public static void main(String[] args) { InsertSort sort = new InsertSort(10,100); sort.SortIt(); } } Copyright Tarena Corporation,2009.All rights reserved } public int getAge() { return age; } public boolean isSex() { return sex; } public int getWeight() { return weight; } } 89.编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”,4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个“。 答: package com.tarena; class SplitString { public static String split(String str,int num) { byte[] strs = str.getBytes(); if(strs[num-1]<0) { num=num-1; } byte[] news = new byte[num]; System.arraycopy(strs,0,news,0,num); return new String(news); } public static void main(String[] args) { String str = split(“我ABC”, 4); System.out.println(str); String str2 = split(“我ABC走DEF”, 6); System.out.println(str2); } } 92、请用JAVA实现两个类,分别实现堆栈(Stack)和队列(Queue)操作。 答:public class MyStack { private List list; public MyStack(){ list = new ArrayList(); } public boolean isEmpty(){ return list.size() == 0; } public void push(Object obj){ list.add(obj); } public Object pop(){ if(list.size()>0){ Object obj = list.get(list.size()-1); list.remove(list.size()-1); return obj; }else{ return null; }} public int getNumber(){ return list.size(); } } class IntegerQueue { public int[] integerQueue; // 用来当队列 public int tail; // 队尾 public int size ;// 队的长度,也可以设置一个默认值,溢出时从新申请 public IntegerQueue(int size) { integerQueue = new int[size]; this.size = size; tail = 0; } public void inQueue(int i) { if (tail < size) { this.integerQueue[tail] = i; tail++; } else { System.err.println(“溢出啦!”); } } public int outQueue() { if (tail >= 0) { int tmp = this.integerQueue[0]; tail–; return tmp; } else { System.err.println(“队列为空!”); throw new RuntimeException(); } } } 作用域 当前类 同包 子类 其它 public √ √ √ √ protected √ √ √ × private √ × × × 字符串倒转 public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println(“Please input a String:”); String st = in.nextLine(); StringBuilder buffer = new StringBuilder(st); st = buffer.reverse().toString(); System.out.println(“The reverse of the string is: “+st); } } 计算日期 DateFormat df = new SimpleDateFormat( “yyyy-MM-dd “); String d = “2005-11-26 “; Calendar c = Calendar.getInstance(); c.setTime(df.parse(d)); //System.out.println(c.get (Calendar.WEEK_OF_MONTH)); System.out.println(c.get(Calendar.DAY_OF_WEEK)); 1. 以某天(a)为基准,计算n天(b)之后是星期几: 假设该天为星期m,则n天之后是星期q: q = m + (n % 7) 2. 一年有365天,2001年的今天是星期2, 2002年的今天是: 2 + 365 % 7 = 2 + 1 = 3, 不信你查日历表。 因为 365 = 7 * x + 1, 因此,每过一年的同月同日星期数便加1。 3. 闰年有366天,因此如果月份大于2,则每过一年的同月同日星期数除了要加1,还要再加间隔的闰年数。 4. 每四年有一个闰年,每一百年要减去一个闰年,每四百年要加回一个闰年。 因此,今天是: (7 – 1) / 4 = 1 (2 + (7 – 1) + 1) % 7 = 9 % 7 = 2, 不信你在窗口右下角的时间上双击鼠标看看是不是星期二。 5. 只要还有一点数学常识,星期的计算公式即可推导出来了。 遍历概念 所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问。访问结点所做的操作依赖于具体的应用问题。 遍历是二叉树上最重要的运算之一,是二叉树上进行其它运算之基础。 遍历方案 1.遍历方案 从二叉树的递归定义可知,一棵非空的二叉树由根结点及左、右子树这三个基本部分组成。因此,在任一给定结点上,可以按某种次序执行三个操作: (1)访问结点本身(N), (2)遍历该结点的左子树(L), (3)遍历该结点的右子树(R)。 以上三种操作有六种执行次序: NLR、LNR、LRN、NRL、RNL、RLN。 注意: 前三种次序与后三种次序对称,故只讨论先左后右的前三种次序。 2.三种遍历的命名 根据访问结点操作发生位置命名: ① NLR:前序遍历(PreorderTraversal亦称(先序遍历)) ——访问结点的操作发生在遍历其左右子树之前。 ② LNR:中序遍历(InorderTraversal) ——访问结点的操作发生在遍历其左右子树之中(间)。 ③ LRN:后序遍历(PostorderTraversal) ——访问结点的操作发生在遍历其左右子树之后。 注意: 由于被访问的结点必是
Java笔试题整合 24页word文档贴出来(七)
} } } } System.out.println(“The ArrayList Sort After:”);