The method of collection and recycling of conventional error

There are three general set of lecture: for circulation, enhanced for loops and iterators. Here mainly to talk about for loops and iterators method.

1, enhanced for loop:

  Note: Because the set is different from the array, the type of elements in a set do not necessarily match, it is necessary to define variables of type Object receiving elements in the collection.

	@Test
	public void Testfor() {
		Collection coll = new ArrayList();
		coll.add(new Date());
		coll.add("AA");
		coll.add(123);
		coll.add("BB");
		coll.add(new String("CC"));

		// 增强for循环
		for (Object i : coll) {
			System.out.println(i);
		}
	}

 2, iterators:

  Note: an iterative method is used collection cycle. [Iteration start, will be understood as 'Pointer' above the first element, the first hasNext () determines a next element exists, then next () output.

     hasNext (): determining whether there is a next element.

     next (): returns the next element.

	@Test
	public void TestIterator() {
		Collection coll = new ArrayList();
		coll.add(new Date());
		coll.add("AA");
		coll.add(123);
		coll.add("BB");
		coll.add(new String("CC1"));
		// 迭代器
		Iterator i = coll.iterator();
		while (i.hasNext()) {
			System.out.println(i.next());
		}
	}

 3, writing error: [error when writing such an approach routine encounter]

    Explanation of the following code: The first understand the next () action, 'pointer' down, returns the next element. [Here the concept does not exist actually pointers, for enhancement of understanding introduced into the C language pointer, the elements can be understood as subscript]

      while in the next first time is determined next () is empty, the operation returns to the fact new Date () elements, while the 'Pointer' moves from the top of the first element to the new Date (). into the circulation () returns the second element 'AA', while the 'pointer' c moves from the first element to the second element.

      While in the second time is determined next () is empty when, in fact, the operation returns to element 123, while the 'pointer' to move from the top to the second element 123 into the cycle after next () Returns a fourth elements 'BB', while the 'pointer' c from the third element to the fourth element.

      Subsequent elements and so on.

    Output:

    AA

    BB

    Throw an exception:

    java.util.NoSuchElementException

 

Summary: 1, while the determination condition is not actually play a role to ensure there is an output element. Because it is not an element is determined and output. Resulting in compiling normal, but the actual output time interval of the output type.

   2, when the last element will be determined while the elements, the output cycle was empty, throw an exception: java.util.NoSuchElementException.

	@Test
	public void TestNext() {
		Collection coll = new ArrayList();
		coll.add(new Date());
		coll.add("AA");
		coll.add(123);
		coll.add("BB");
		coll.add(new String("CC2"));
		
		// 错误写法
		Iterator i = coll.iterator();
		while (null != i.next()) {
			System.out.println(i.next());
		}
	}

 

Guess you like

Origin www.cnblogs.com/honny-seven/p/11332249.html