CS 251 Assignment 1&2 知识点总结与注意

这篇博客总结了中级软件设计课程中关于CS 251 Assignment 1和2的内容,重点在于Java泛型数组的实现。作者提到了在实现过程中的一些关键知识点和编程规范,包括大项的总结和代码内部的具体注意事项。
摘要由CSDN通过智能技术生成

中级软件设计课程作业1、2:知识点总结与注意

首先为了参照起来方便并且能够把握整体,这里就不顾及文章长度的先把整个作业全部的代码贴上,可以直接略过,以后看到后面有不清楚的内容再返回来。
 

整个作业就是实现一下Java泛型数组的几个自带功能。大的Tip,注意或者知识点在全部代码后面总结,小的注意就在代码里用中文提及:

package vandy.cs251;

import java.lang.ArrayIndexOutOfBoundsException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * Provides a generic dynamically-(re)sized array abstraction.
 */
public class Array<T extends Comparable<T>>
             implements Comparable<Array<T>>,
                        Iterable<T>,
                        Cloneable {
    /**
     * The underlying array of type T.
     * The current size of the array.
     * Default value for elements in the array.
     */
    private T[] myArray;    //Initiative the generic array
    private int currentSize = 0;    //Set up the current size
    private T defaultValue;    //Set up the default value of every elements of arrays

自己定义的变量别忘了考虑前面的关键词是用public、protect、private还是省略。TA第一眼就强调了这里的问题,可以看出这对于科班出身的程序员来说很重要。一般情况下,最为保险的就是将无需全局的变量限定为private。

 
 
    /**
     * Constructs an array of the given size.
     * @param size Non-negative integer size of the desired array.
     * @throws NegativeArraySizeException if the specified size is
     *         negative.
     */
    @SuppressWarnings("unchecked")
    public Array(int size) {
        if (size < 0)
            throw new NegativeArraySizeException();    //An array's length can't be negative
        myArray = (T[]) new Comparable[size];    //Must write in this way
        //myArray = (T[]) new Object[size];
        currentSize = size;
    }

    /**
     * Constructs an array of the given size, filled with the provided
     * default value.
     * @param size Non-negative integer size of the desired array.
     * @param mDefaultvalue A default value for the array.
     * @throws NegativeArraySizeException if the specified size is
     *         negative.
     */
    @SuppressWarnings("unchecked")
    public Array(int size,
                 T defaultValue) {
        this(size);    //Using the constructor Array(int size)
        Arrays.fill(myArray, defaultValue);    //Fill all the elements of array with the default value
        this.defaultValue = defaultValue;    //update defaultValue and currentSize
        currentSize = size;
    }

    /**
     * Copy constructor; creates a deep copy of the provided array.
     * @param s The array to be copied.
     */
    @SuppressWarnings("unchecked")
    public Array(Array<T> s) {
        //Copies the specified array,
        // truncating or padding with nulls (if necessary)
        // so the copy has the specified length.
        myArray = Arrays.copyOf(s.myArray, s.currentSize);
        this.currentSize = s.currentSize;    //Remember s is a object
        this.defaultValue = s.defaultValue;
    }

    /**
     * Creates a deep copy of th
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值