Vector usage of Java

Source: Internet
Author: User
Tags tostring

Vector usage is simple, which has been demonstrated in the previous example. Although most of the time we simply insert the object with AddElement (), we extract one object at a time with ElementAt () and get an "enumeration" of the sequence using elements (). However, there are still a number of other methods that are very useful. Like our usual practice with the Java library, we don't use or tell all of these methods here. However, be sure to read the appropriate electronic documents and have a general idea of their work.

1. Crash Java
The Java standard collection contains the ToString () method, so they can generate their own string representations, including the objects they hold. For example, in a vector, ToString () steps and iterates through the various elements of the vector and calls ToString () for each element. Suppose we want to print out the address of our class now. It seems as if a simple reference to this can be (especially the C + + Programmer's inclination to do so):

: Crashjava.java
//One way to crash Java
import java.util.*;

public class Crashjava {public
  String toString () {return
    ' Crashjava address: ' + this + ' \ n ';
  }
  public static void Main (string[] args) {
    vector v = new Vector ();
    for (int i = 0; i < i++)
      v.addelement (New Crashjava ());
    System.out.println (v);
  }
} ///:~

If you simply create a Crashjava object and print it out, you will get an endless series of offending errors. However, if the Crashjava object is placed in a vector and the vector is printed as shown here, there is no hint of error, or even a violation will not appear. At this point Java simply crashes (but at least it does not crash my operating system). This has been tested in Java 1.1 through.
At this point, the automatic type conversion of the string is occurring. When we use the following statement:
"Crashjava Address:" + this
The compiler finds a "+" and something else that is not a string after a string, so it tries to convert this to a string. The conversion is called ToString (), which produces a recursive call. If this happens within a vector, it looks like the stack will overflow, and the control mechanism has no chance to respond at all.
If you do want to print out an object's address in this case, the solution is to invoke the ToString method of object. You do not need to join this at this time, just use super.tostring (). Of course, there is a prerequisite for this: we must inherit directly from object, or no parent class overrides the ToString method.

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.