Java中常见的基础类(三)

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。Java中常见的基础类(三),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

System类

System类有三个属性比较常用:

/*this stream corresponds to keyboard input or another input source specified bythe host environment or user*/
//这个流可以作为键盘输入和其他的信息输入源
public final static InputStream in = null;
/*This stream is already
     * open and ready to accept output data. Typically this stream
     * corresponds to display output or another output destination
     * specified by the host environment or user.*/
//这个流由用户定义来进行一些内容的输出
public final static PrintStream out = null;
/* convention, this output stream is used to display error messages
     * or other information that should come to the immediate attention
     * of a user even if the principal output stream,*/
//这个流可以做一些紧急的需要提示用户的信息的输出
public final static PrintStream err = null;

这三个流的使用方法如下:

System.out.println("out");
System.err.println("err"); //打印的是红色的输出信息
//System.in和Scanner连用,用来获取用户的输入
Scanner scanner = new Scanner(System.in);

Java集合类

Java集合类的根接口是Collection接口,Collection接口下面有List和Set接口

Collection
    |- List
    	|-ArrayList
    	|-LinkedList
    |- Set
    	|-HashSet

集合类的常见API操作:

int size(); //返回集合元素个数
boolean isEmpty(); //判断是否为空
boolean contains(Object o); //判断是否包含元素o
Iterator<E> iterator(); //迭代器:完成集合元素的迭代
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);

关于Java集合,有一个比较需要注意的点,就是关于集合元素的迭代

Java集合元素的迭代是通过iterator()方法返回一个Iterator对象来完成,然后使用这个对象来进行迭代

List<Integer> list = new ArrayList<>();
list.add(1);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
    Integer n = iterator.next();
    System.out.println(n); //1
}

Iterator对象有两个重要的方法,hasNext()和next()方法,对于ArrayList而言,这两个方法可以在ArrayList的子类ListItr和子类Itr中看到具体的方法实现

我们从下面这段代码往下探索:

ArrayList<Integer> list = new ArrayList<>();
Iterator<Integer> iterator = list.iterator();

在这里点进iterator()方法,观察ArrayList如何产生迭代器对象

public Iterator<E> iterator() {
   		//这是ArrayList自己的子类,这个子类继承了Iterator接口
        return new Itr();
}

看看Itr子类的源码,里面可以看到上面两个重要方法的实现

private class Itr implements Iterator<E> {
    	//下一个元素的下标,初始值为0
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
    	//这个expectedModCount用来记录元素在迭代之前的长度,如果在迭代期间集合的元素被其他线程使用集合类中的方法修改
    	//会报异常: ConcurrentModificationException
        int expectedModCount = modCount;
    
		//判断当前下标是否是最后一个元素的下标
        public boolean hasNext() {
            return cursor != size;
        }
		
        @SuppressWarnings("unchecked")
        public E next() {
            //这里面对里面的元素长度进行检验,就是报 ConcurrentModificationException 的地方
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }
    // ... 其他代码都暂时省略
}

篇幅有限,从源码中可以看到hasNext()方法主要判断当前集合中是否有元素,而对于next()方法,可以精简成如下代码:

 public E next() {
     		//定义临时变量 i = 当前指针位置
            int i = cursor;
            // 当前指针向移位
            cursor = i + 1;
     		//返回下标为i的元素,这时候cursur已经后移一位
            return (E) elementData[lastRet = i];
        }

可以看到hasNext()方法就是做了两件事:

1.  返回当前指针的元素

2.  当前指针往后移一位

总而言之:对于Iterator对象,利用这两个方法就可以实现集合元素的迭代!!!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/202492.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!