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;
}
   答:contains()方法就是用来遍历Object里的数组,如果找到与标记相同的值则返回所在位置,否则返回false。

1.2 解释E remove(int index)源代码

//remove代码
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;
} //rangeCheck代码
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
 答:这个代码大概就是在做一个删除元素,位置前移的一个操作。但是首先要先判断一下所传入的参数是否超出原来数组的长度,若超出则抛出异常
否则将元素删除,后面的元素前移,原来最后元素的位置赋予null 。

1.3 结合1.1与1.2,回答ArrayList存储数据时需要考虑元素的类型吗?

 答:不需要,就像1.1所用的是Object下的数组,那就说明Object下所有的对象都可以使用。

1.4 分析add源代码,回答当内部数组容量不够时,怎么办?

 public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
} /**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size); ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
  答:当数组容量不够用时,会自动加长数组的长度。

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 void RangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
} /**
* Save the state of the <tt>ArrayList</tt> instance to a stream (that
* is, serialize it).
*
* @serialData The length of the array backing the <tt>ArrayList</tt>
* instance is emitted (int), followed by all of its elements
* (each an <tt>Object</tt>) in the proper order.
*/
    答:正如所贴代码中的解释,`RangeCheck`方法只是用来检测是否超出范围,这个只是内部的事情,外部也没用到,只
需定义为private就可以了,不需要定义为public。

2、HashSet原理

2.1 将元素加入HashSet(散列集)中,其存储位置如何确定?需要调用那些方法?

  答:Set变量——HashSet对象——根据哈希算法计算出散列码,找到存储位置——根据相应的哈希码,找到对应的桶

3、ArrayListIntegerStack

题集jmu-Java-05-集合之5-1 ArrayListIntegerStack

3.1 比较自己写的ArrayListIntegerStack与自己在题集jmu-Java-04-面向对象2-进阶-多态、接口与内部类中的题目5-3自定义接口ArrayIntegerStack,有什么不同?(不要出现大段代码)

  答:5-1使用list可以自动改动容量,而4-3使用的数组,规定了大小,要改容量的时候比较麻烦,而且需要使用到指针。

3.2 简单描述接口的好处.

   答:所谓接口,就是可以将两个毫不相干的两个东西连接在一起,以这题为例,它们都有IntegerStack接口,使用相同的方法名ArrayListIntegerStack,
但是这两题所实现的方法不同,即用一个方法可以有不同的实现。

4、Stack and Queue

4.1 编写函数判断一个给定字符串是否是回文,一定要使用栈,但不能使用java的Stack类(具体原因自己搜索)。请粘贴你的代码,类名为Main你的学号

public class Main201521123093 {
public static void main(String[] args) {
Stack<Character> stack = new Stack<Character>();
Scanner sc = new Scanner(System.in);
String str = sc.next();
for (int i = 0; i < str.length(); i++) {
stack.push(str.charAt(i));
}
for (int j= 0; j < str.length(); j++) {
if (stack.pop() != str.charAt(j)) {
System.out.println("不是回文");
break;
}
else {
System.out.println("是回文");
break;
}
}
}
}

4.2 题集jmu-Java-05-集合之5-6 银行业务队列简单模拟。(不要出现大段代码)

5、统计文字中的单词数量并按单词的字母顺序排序后输出

题集jmu-Java-05-集合之5-2 统计文字中的单词数量并按单词的字母顺序排序后输出 (不要出现大段代码)

while(sc.hasNext()){
String word = sc.next();
if(word.equals("!!!!!")){
break;
}
set.add(word);
}
System.out.println(set.size());
for(int i =0;i<10;i++){
System.out.println(set.toArray()[i]);
}

5.1 实验总结

  答:刚开始没有头绪,不知道从哪开始,后面用treeset才知道原来这题的代码这么少。。。。
treeset中本身就有排序的功能,所以就不需要再写排序的代码了。

7、面向对象设计大作业-改进

7.1 完善图形界面(说明与上次作业相比增加与修改了些什么)



7.2 使用集合类改进大作业

参考资料:

JTable参考项目

3. 码云上代码提交记录及PTA实验总结

题目集:jmu-Java-05-集合

3.1. 码云代码提交记录

在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

最新文章

  1. win10用户文件夹重命名,启用administrator账户,删除文件夹时提示找不到该项目
  2. Windows on Device 项目实践 2 - 感光灯制作
  3. 设置tomcat内存设定
  4. 通过ajax访问Tomcat服务器web service接口时出现No &#39;Access-Control-Allow-Origin&#39; header问题的解决办法
  5. Oracle中建立表
  6. org.apache.catalina.session.StandardManager doLoad
  7. UVA11806-Cheerleaders(容斥原理+二进制)
  8. OpenGL ES 2.0 纹理映射
  9. re2c实例
  10. UITableView控制头部View
  11. CF/div2c/贪心
  12. 解决NSTimer循环引用Retain Cycle问题
  13. vs编译过程中出的错
  14. Python_查找员工信息-48
  15. opencv学习之路(23)、轮廓查找与绘制(二)——访问轮廓每个点
  16. ORM框架EF
  17. mysqldump导出数据时,某些表不导出,排除某些表,不导出某些表
  18. K8S 安装笔记
  19. nonlocal和global
  20. c# SSH ,SFTP

热门文章

  1. HTTP 0.9 / 1.0 / 1.1
  2. Matlab学习笔记(1)
  3. 谈谈php依赖注入和控制反转
  4. 为什么eclipse修改程序之后运行的时候还是前一版本的内容?
  5. java静态内部类理解
  6. Mybatis分页插件PageHelper正确的使用方法(网上有2篇不够科学的文章)
  7. 使用TenforFlow 搭建BP神经网络拟合二次函数
  8. find the Nth highest salary(寻找第N高薪水)
  9. MySQL使用聚合函数查询
  10. Java基础---其他对象