If a program contains only a fixed number of objects,And know how long they exist,Then this procedure is quite simple.
There are many ways to accommodate objects,Arrays are one of the most common.It mainly has the following two characteristics:efficiency and type.For Java, the most efficient way to save and access a series of objects (actually object handles) is an array.The array is actually a simple linear sequence,So the access speed is very fast,But it also has some limitations,If the size of the array is fixed,And it must not change within the "time of existence."
For an array of primitive data types,Its operation type is similar to the object array,The difference is that the former does not hold a handle to the object,It's the actual value.
When programming,It is usually not known exactly how many objects need to be saved,Sometimes I even want to save objects in more complex ways,To solve such problems,Java provides several "set classes":vector, bitset, stack, and hashtable.
Disadvantage of collections:Type information is lost.It actually holds a handle to an object of type object.
Save the object with a collection and then visit,You need to know the exact type of the objects in the collection in advance,Otherwise, an exception will occur during use.And iterators can solve this problem.An iterator is an object,Its role is to traverse a series of objects,And select every object in that sequence,Also keep client programmers from knowing or paying attention to the underlying structure of that sequence.
Enumeration in Java is a typical iterator,It is mainly used to do the following things:
(1) A method called elements is required for the collection to provide us with an enumeration. When its nextelements are called for the first time, this enumeration returns the first element in the sequence.
(2) Use nextelements to get the next object.
(3) Use hasmoreelements to check if there are more objects.
The vector class implements a growable array of objects.As with arrays,It contains components that can be accessed using integer indexes.However, the size of the vector can be increased or decreased as needed,To accommodate adding or removing items after the vector is created.
A bitset is actually a vector of "binary bits". If i want to efficiently save a lot of "on-off" information, you should use a bitset. Each component of the bit set has a boolean value. Index the bits of the bitset with a non-negative integer.Each indexed bit can be tested, set, or cleared.By logical AND, logical OR and logical XOR operation,You can use one bitset to modify the contents of another bitset. by default,All bits in the set have an initial value of false.
A stack is also called a "last in, first out" collection. The stack class in java inherits from the vector class, which extends the class vector with five operations, allowing the vector to be treated as a stack.It provides the usual push and pop operations, as well as the peek method to take the apex of the stack, the empty method to test if the stack is empty, the search method to find items in the stack and determine the distance to the top of the stack.
Hash tables are an important storage method.It is also a common retrieval method.The basic idea is to use the value of the relation code as an independent variable.Calculate the corresponding function value through a certain functional relationship,Interpret this value as the storage address of the node,The node is stored in a storage unit corresponding to the calculated storage address.The retrieval method is used to retrieve the key code.Hash tables now have a complete set of algorithms for inserting, deleting, and resolving conflicts.Hash tables are used to store objects in java.Achieve fast retrieval.
to sum up
- The bridge pattern in Java-an example of the object structure pattern
- Detailed method of getting Class object through Class in Java
- Parsing Json data using Java (nesting of arrays of objects)
- Java write object to file read_serialization and deserialization examples
- Java list object list to achieve deduplication or take out and sort method
- Example of java object input and output stream read and write files
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- android studio - unresolved reference comes out in kotlin
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- python - importerror: cannot import name md5 error cannot be resolved