JAVA Notes (ix)

Source: Internet
Author: User

  1. Next () in the iterator class is the get element and moves to the next position. [so use to be careful, do not use casually, because each use is to access an object] The return type is object is really e (generic) hasnext () if there are still elements that can iterate, return ture.
  2. Iterators exist as dependent collections, otherwise they are meaningless.
  3. 1. Create a Collection Object 2. Create an Element object 3. Add an element to the collection 4. Iterate through the collection (Gets the iterator object through the collection object, and then through the Hasnext () and Next () methods)
  4. An iterator-specific implementation class is a real, concrete subclass that is implemented in the form of an inner class. "These collections are generally related to the Util package".
  5. The List (ArrayList, Vector, LinkedList) collections are repeatable and orderly (the order here is how to sequence them in order, because it is like an index) and set (Hashset,treeset) It is not possible to have duplicate elements.
  6. The list collection is unique to the method: You can add elements at the specified location (you can immediately follow this bit, and the exception will appear). The Set () modify element "modifies the element according to the index, returning the modified element".
  7. The unique traversal of the list collection can be done through the size () and get () methods.
  8. Listiterator (list iterators) inherit iterator, so you can traverse them directly using the Haxnext () and Next () methods. Its unique functionality can be reversed (previous () Get elements and hasprevious () to determine if there are elements), but the reverse traversal must be preceded by a forward traversal, otherwise meaningless (because the pointer is still at the beginning, not moving)
  9. Iterators are a collection of dependencies, data is added to the collection, but iterators do not know, so the error (concurrency modification exception) is actually the iterator traversing the element, through the collection cannot modify the element. Workaround: A; iterator iteration element, iterator modifies element "Listiterator" "The method element is just after the element of the iteration." B; set iterates over elements, sets modify elements. (through for Loop) "The method element is last added"
  10. Arrays: Query fast (via index) but add and delete complex. A chain of data (nodes) consisting of multiple nodes connected by a chain: data and address components (data fields and pointer fields, and the address of the next element in the Pointer field).
  11. The list lookup is slow, but increases the deletion block.
  12. List of the three character classes: ArrayList (the underlying data structure is an array, query fast, delete and subtract slow, thread unsafe, high efficiency "asynchronous") vector: the underlying data structure is an array, query fast, delete and subtract slow, thread safety, low efficiency (now rarely used, there are alternative classes) LinkedList: The underlying data structure is a linked list, the search is slow, and delete fast, thread unsafe, high efficiency.
  13. Vector-specific features: addelements () Add function ElementAt (): Gets the position of the element, elements () traversal. But these methods have been replaced by the later, rarely used, understand.
  14. LinkedList's AddLast () method, like Add (), is nothing special, so it's rarely used.
  15. The Contains () method relies on the Equals () method, so when you compare content, you override the Equals () method, otherwise the Equals () method in the parent class object is called by default, so the address value is compared.
  16. The collection is a reference type, and we sometimes see the basic type, which is actually boxed (the valueof () method can be used to replace the base type with the wrapper type).
  17. Collections also mimic the practice of arrays, which are called generic techniques when creating objects that explicitly define the data type of the element.
  18. Generics: A special type that is explicitly deferred until the object is created or when the method is called. The parameterized type passes the type as a parameter. Format < data type >[The data type here refers to the reference type]
  19. The benefits of generics: A; Advance the issue of the run-time to a compile-time B; Avoid forcing type conversion C; program design is optimized to avoid yellow warnings.
  20. In the early days, we used the object class to represent any type, the upward transformation is no problem, but the downward implication of the type conversion problem, so it is not safe, in the JDK5 after the proposed generic "improve the security of the program."
  21. Generic definition in class public class name < generic type,.....> generic definition in method public < generic type > return type method name < generic type > "What is Type" generic definition in Interface: public Interface interface < generic type 1,......>
  22. Generics must be the same type on both sides of the equals sign when explicitly written.
  23. Generic advanced: 1.? : Represents any type, if not explicit, is the object class and any Java class. 2.? Extends e: down qualification, "E and its subclass" 3.? Super e: Limits "E and its parent class" up.
  24. New features of Jdk5: Automatic disassembly box, generics, enhanced for, static import, variable parameters, enumeration.
  25. Enhanced for format for (element data type variable: array or set) {output variable, variable is element. }
  26. The target of the enhanced for is not NULL, otherwise an error occurs, so use the enhanced for to determine if it is null. After judging is used.
  27. In fact, an enhancement for is a substitute for an iterator.
  28. Static Import: Import static package name ... Class name. method name direct import to the level of method Note: The method must be static, and if the method name is the same, prefix it (these things are seldom used, understand)
  29. The format modifier for a mutable parameter returns a value type method name (data type ... Variable name) {}. Note: The underlying variable here is actually an array. If a method has more than one parameter, the variable parameter must be the last.
  30. public static <t>list<t>aslist (T...A) transforms the array into a collection, but note that although you can change the array to a collection, the length of the collection cannot be changed. So adding and removing is useless, but modifications are possible.
  31. Go to Heavy code (this is more cumbersome, rarely used, but to understand it)

Import java.util.ArrayList;

public class Demo {

public static void Main (string[] args) {

arraylist<string> list = new arraylist<string> ();

List.add ("Hello"); List.add ("World"); List.add ("Java");

List.add ("World"), List.add ("World"), List.add ("World"), List.add ("World");

List.add ("Java"), List.add ("Hello"), List.add ("World"), List.add ("Java"), List.add ("World");

for (int x = 0; x < list.size ()-1; x + +) {

for (int y = x + 1; y < list.size (); y++) {

if (List.get (x). Equals (List.get (y))) {

List.remove (y);

y--;//This place to note, if the element is connected and the same, then the previous step although removed, but the next is still the same, so that there is a slip, in order to solve the problem, let it minus one, to do it again.

}

}

}

for (String s:list) {

System.out.println (s);

}

}

}

    1. The collection of data structures using the LinkedList simulation stack, this encapsulation idea is very good, to learn its essence. "It's very rare, but it's a good idea."

Package Hhxu.lianxi;

import java.util.LinkedList;

Public class utiltest {

Private linkedlist link;

Public Utiltest () {

link=New LinkedList();//creating Utiltest objects is actually linkedlsit creating objects

}

Public void Add (Object obj) {//increment

Link.addfirst (obj);//The LinkedList method is called in the method body

}

Public Object get () {//Remove

return Link.removefirst ();

}

Public boolean IsEmpty () {//To determine whether it is empty

return link.isempty ();

}

}

Package Hhxu.lianxi;

Public class Teststack {

Public Static void Main (string[] args) {

Utiltest ut=New utiltest ();

Ut.add ("Hello");

Ut.add ("World");

Ut.add ("Java");

while (!ut.isempty ()) {

System. out. println (Ut.get ());

}

}

}

    1. Package Division principle: A; According to the function of B; by module C; First according to the module, then according to the function points.
    2. Remember one principle: when there are fewer parameters, write the arguments and pass an object when there are more arguments.
    3. When multiple objects share a member variable, use a static
    4. If there is a switch inside the loop and there is a break in the switch, the end is not the loop, but the switch statement.
    5. Do not let others create the object to write the constructor method to the private class name () {}. Then the method is written as static.
    6. The set collection cannot have duplicate elements and is unordered (the storage order and the fetch order are inconsistent). Note that the set set is unordered, but as a set it must have its own order of storage, and if your order happens to be just like it, it is coincidental, it cannot be ordered, and a few more data will find the result.
    7. HashSet Why do I store strings with the same string content that can only store one? By looking at the source of the Add method, it is true that the bottom of the method relies on both hashcode () and Equals (): Step: First compare the hash value, if the same, continue to compare the address value or equals (), if the difference is directly added to the collection. Follow the steps of the method: First look at the value of Hashcode () is the same, the same equals () method, return ture, explain the element is repeated, do not add, and vice-Add. The difference is added directly. If the class does not override both methods, the default is in the object class, which is generally not the same, and the string class overrides both methods, so only one is reserved for the same element. "Extension: This illustrates a problem if the HashSet collection has the same elements as a workaround, overriding the Hashcode () and Equals () methods in the parent class object."
    8. The hashcode values of different elements are generally not the same, but sometimes they are the same, which is why the comparison of Hashcode () is followed by a comparison of Equls ().

JAVA Notes (ix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.