1. 本周学习总结
2. 书面作业
1.ArrayList代码分析
1.1 解释ArrayList的contains源代码
//contains()方法
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
//indexOf()方法
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
**答:这段代码的主要目的是判断在对ArrayList遍历时所用的方法,在输入参数o为null时,判断elementData[i]是否为null,如果是则返回i,
不为null时,使用equals方法。**
1.2 解释E remove(int index)源代码
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
**答:这段代码的目的是用rangeCheck方法判断输入的参数index是否超出大小范围,如果是,则输出IndexOutOfBoundsException异常,
如果不是,则删除指定的元素,之后再将删除位置之后的元素往前移,最后使得size-1的位置的值为null。
**
1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?
答:ArrayList存储数据时需要考虑元素,虽然平时我们在使用ArrayList时,像String类、Integer类、Double类等这些基础数据我们都是直接使用,
但是在放入一些特定的自定义类的话就需要重写equals方法。
1.4 分析add源代码,回答当内部数组容量不够时,怎么办?
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
答:这段代码是为了利用ensureCapacityInternal来调整容量。
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
答:判断是否超出容量,如果是,则用gorw方法增加。
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
答:增加原来容量的一半(右移一位就是/2),也就是说新List的容量是旧的1.5倍,且把旧数组拷贝至新数组。
1.5 分析private void rangeCheck(int index)源代码,为什么该方法应该声明为private而不声明为public?
/**
* Checks if the given index is in range. If not, throws an appropriate
* runtime exception. This method does *not* check if the index is
* negative: It is always used immediately prior to an array access,
* which throws an ArrayIndexOutOfBoundsException if index is negative.
*/
答:private声明只在代码内部进行,用户获取不到,且这个函数的目的是限制语句的操作范围,防止越界。所以使用private声明。
2.HashSet原理
2.1 将元素加入HashSet(散列集)中,其存储位置如何确定?需要调用那些方法?
答:HashSet是通过HashMap实现的,使用了hashCode方法来确定储存位置。
3.ArrayListIntegerStack
题集jmu-Java-05-集合之5-1 ArrayListIntegerStack
3.1 比较自己写的ArrayListIntegerStack与自己在题集jmu-Java-04-面向对象2-进阶-多态、接口与内部类中的题目5-3自定义接口ArrayIntegerStack,有什么不同?(不要出现大段代码)
答:首先在第四次实验中自定义接口ArrayIntegerStack使用了内部数组,在增添和删除上比较麻烦,而第五次实验中,
ArrayListIntegerStack则是定义了ArrayList这个动态数组,可以自动扩容,所以不用考虑大小的问题。
3.2 简单描述接口的好处.
答:接口自己本身有定义的方法,使用接口就可以不需要重复写。
4.Stack and Queue
4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号。
import java.util.LinkedList;
class Stack<T> {
private LinkedList<T> storage = new LinkedList<T>();
public void push(T v) {
storage.addFirst(v);
}
public T peek() {
return storage.getFirst();
}
public T pop() {
return storage.removeFirst();
}
public boolean empty() {
return storage.isEmpty();
}
public String toString() {
return storage.toString();
}
}
public class Main201521123049 {
public static void main(String[] args) {
Stack<Character> stack = new Stack<Character>();
String string = "201521125149";
for (int i = 0; i < string.length(); i++) {
stack.push(string.charAt(i));
}
boolean flag = true;
for (int i = 0; i < string.length(); i++) {
if (stack.pop() != string.charAt(i)) {
flag = false;
break;
}
}
if (flag) {
System.out.println("是回文字符串");
} else {
System.out.println("不是回文字符串");
}
}
}
4.2 题集jmu-Java-05-集合之5-6 银行业务队列简单模拟。(不要出现大段代码)
//题目中分为两个窗口,所以我们先要区分号码的奇偶
for (int i = 1; i <= n; i++) {
int x = scanner.nextInt();
if (x % 2 == 0) {
A.offer(x);
} else {
B.offer(x);
}
}
//接下来就是按题目要求,进行排序
while(!A.isEmpty() || !B.isEmpty())
{
Integer a = A.poll();
if(a1 != null){
if(B.isEmpty() && A.isEmpty())
System.out.print(a);
else
System.out.print(a + " ");
分完后按方法排序
5.统计文字中的单词数量并按单词的字母顺序排序后输出
题集jmu-Java-05-集合之5-2 统计文字中的单词数量并按单词的字母顺序排序后输出 (不要出现大段代码)
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<String>set=new TreeSet();
Scanner in=new Scanner(System.in);
while(in.hasNext()){
String s=in.next();
if(s.equals("!!!!!"))break;
set.add(s);
}
System.out.println(set.size());
if(set.size()<10)
for (String string : set) {
System.out.println(string);
}
else{
for (int i = 0; i < 10; i++) {
System.out.println(set.toArray()[i]);
}
}
}
}
5.1 实验总结
首先这个实验是在实验课是拍老师的代码,不是自己写的,只不过自己在对照题目理解下,这道题主要是用了TreeSet方法直接进行排序。
7.面向对象设计大作业-改进
7.1 完善图形界面(说明与上次作业相比增加与修改了些什么)
7.2 使用集合类改进大作业
参考资料:
JTable参考项目
3. 码云上代码提交记录及PTA实验总结
题目集:jmu-Java-05-集合
3.1. 码云代码提交记录
•在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图
3.2. PTA实验
•编程(5-1, 5-2, 5-3(选做), 5-6)
•实验总结已经在作业中体现,不用写。