Q: Array creation?

**A:** There are two types of data in Java, basic types and object types, and in many programming languages (even object-oriented language C + +), arrays are basic types. In Java, however, the array is viewed as an object. Therefore, when you create an array, you must use the new operator:

Null//defines a reference to an arrayint[+//creates the array, and sets Objarray to refer T o It

or using an equivalent single statement declaration and definition:

intint[+];

The [] operator is a flag for the compiler, which indicates that an array object is being named instead of a normal variable. Since the array is an object, its name (as in this example, Objarray) is a reference to the array, which is not the array itself. The array is stored in an address in the heap space, and Objarray only holds the address.

The array has a length property that lets you know the size of the current array.

As with most programming languages, once an array is created, the size of the array cannot be changed.

Q: Array initialization list?

**A:** create an array of objects as follows:

Autodata[] cars = new autodata[400];

Unless specific values are assigned to the array's data items, they are always special null objects. If you attempt to access an array data item that contains null, the program will see a run-time error of NULL Pointer assignment, primarily to ensure that a data item is assigned a value before it is read.

Use the following syntax to initialize an array of a primitive type:

Int[] NArray = {0, 3, 6, 9, 12, 15, 18, 21, 24, 27};

The above statement can be surprisingly simple, replacing both the reference declaration and the use of new to create an array. The data in curly braces is called the initialization list. The size of the array is determined by the number of list data items.

Q: An example of an array for process programming?

**A:** first introduce an old-fashioned process-oriented programming (POP) version, and then introduce an object-oriented programming (OOP) version that can achieve the same effect.

The idea of POP and OOP can be consulted in the blog "Understanding of Programming Ideas" (POP,OOP,SOA,AOP)

Example: Arraytest.java

**A:** data items stored in a structure usually contain several fields, so they should be represented by objects rather than by simple types. In this example, in order to simplify, the data type stored in the array is a long type.

**A:** in the search process, using Searchkey to compare with the data items in the array, if the loop variable i changes to the last data item, but there is still no match, the value is not in the array.

**A:** during the deletion process, when the deleted data item is found, move forward all the items with the subscript larger than it to fill the "pit" left after deletion and subtract number one.

Q: An example of an array of object-oriented programming (a)?

**A:** The above example is an old-fashioned process-oriented program, so how do you change it to an object-oriented programming program? The first thing to do is to separate the data store (array) from the program, the rest of the program becomes the user who uses the structure, and the second is to improve the storage structure and communication between the users.

**A:** now wrap this array in a class called Lowarray. The Lowarray class can be thought of as a tool, a user of a tool in a Lowapptest class, and now the program is divided into two different character classes, which is a key first step in writing an object-oriented program.

Example: Lowarray.java, Lowarraytest.java

**A:** The classes used to store data objects are sometimes referred to as container classes (container Class), such as the Lowarray class, where container classes not only store data, but also provide methods for accessing data and other complex operations such as sorting.

Q: An example of an array of object-oriented programming (II)?

**A:** The above example just shows how to divide a program into classes, but it does not give us much real value, the next thing is to reassign the responsibilities between classes, so that you can get more of the benefits of OOP.

**A:** The user of the data storage structure in the above example must know the subscript of the array, the SetElement () and GetElement () methods are still at a low level, and they are no different from the [] operator in the normal Java array. However, some users simply need to randomly access the array data items and do not have to know the subscript of the array, so he will not realize that array subscripts are useful or relevant.

Example: Higharray.java, Higharraytest.java

**A:** The Higharray program gives an improved interface to the data structure, the class user (Higharraytest) uses the interface to no longer consider subscript, it cancels the setelement () and GetElement () method, instead of insert (), find () and delete (). Because the class is responsible for handling subscript issues, these methods no longer need to use subscripts as parameters. Class users can focus on what they are doing rather than how to do it: what to insert, delete, and access, rather than how to perform these operations.

**A:** Please note how short and simple the main () is. The details that must be handled by main () in Lowarray are now resolved by the methods in the Higharray class. For example, the Insert () method places a new data item on the next empty position of the array, and a field named Msize tracks the number of data items that are actually in the group. The main () method no longer needs to worry about how many data items are still in the array.

**A:** Surprisingly, the class user doesn't even have to know what data structure is used in the Higharray class to store the data, and the structure is hidden behind the interface.

Q: Java example of an ordered array?

**A:** Suppose that the data items in an array are sorted by keyword, which is called an ordered array? Why do you want to sort it? One of the benefits is that you can significantly improve the search speed by using binary lookups.

**A:** the Java code for an ordered array is discussed below, and the core of the Orderarray class is the Find () method, which allows binary lookups to locate a particular data item.

Example: Ascorderarray.java, Orderedarraytest.java

The Find () method finds specific data items by dividing the range of array data items in half, and this method sets the variable NL and NR at the beginning to point to the leftmost and rightmost non-empty data items of the array. Then in the while loop, the current subscript NC is set to the middle value of this range.

If you are lucky, the NC may point directly to the data item you want, so you should see if it is equal, and if so, it means that the data item is found, and the next step is to return its subscript NC.

Each step in the loop shrinks the range by half, and eventually the range is small enough to be split (when NL equals NR, the range is a data item, so it needs to be cycled again), and when the range is no longer valid, the lookup is stopped, but the required data item is not found, so returning subscript-1 indicates failure.

Q: What are the advantages and disadvantages of ordered arrays?

**A:** The advantage: The search speed is much faster than the unordered array.

Disadvantage: The insertion operation is slow because all the trailing data items need to be moved to make room.

The delete operations in both ordered and unordered arrays are slow because the data item must move forward to fill the "pit" of the deleted data item.

Q: An example of an array of storage objects?

**A:** In the Java example above, a simple variable of type long is stored in the data structure, which simplifies the program, but it is not representative of how the data storage structure is used in the real world, and often the data records we store are a combination of many fields. For example, an employee record needs to store last name, first name, age, Social security number, and so on.

The following Java example shows how an object is stored and is no longer a variable of a simple type. Using generic technology here, the code can be reused more highly.

Example: Objectarray.java, Person.java, Objectarraytest.java

Q: Large o notation?

**A:** when comparing algorithms, it should be said that something like "algorithm A is twice times faster than algorithm B", but actually this kind of statement doesn't make much sense. Why is it? This is because the corresponding proportions also change radically when the number of data items changes. It is possible that the data item is increased by 50%, A is 3 times times faster than B, or there may be only half of the data items, but now the speed of A and B is the same.

**A:** What we need is a comparison that describes how the algorithm's speed is associated with the number of data items, which is used for the large O notation.

Q: What is the insertion time of unordered arrays?

**A:** Regardless of the number of data items in the array n how large, one insert always at the same time, the new data item is always placed in the next empty place, that is array[size++]. We can say that the time t to insert a data item into an unordered array is a constant k:

T = K

**A:** in the real world, the actual time required to insert (whether nanosecond, fine, or other units) is related to the following factors: microprocessor, execution efficiency of the compiled executable, and so on. The constant k in the above equation contains all of these factors, and in reality, to get the value of K, it is necessary to measure the time spent in an insertion. K is equal to this time.

Q: How long is the array linear lookup?

**A:** in linear lookups of array data items, the average number of comparisons required to find a particular data item is half the total of the data item. That is T = K * (N/2). K is the above-mentioned constant, and merging 2 into K can be a more convenient formula. The new K ' value equals the original K divided by 2. The new formula is:

T = K ' * N

This equation shows that the average linear lookup time is proportional to the size of the array.

Q: What is the binary search time of an array?

**A:** the two-point lookup formula is as follows:

T = K * LOG2 (N)

The time t is proportional to the logarithm of the base N of 2.

Why is the logarithm of the base 2?

When we were kids, we used to guess the number game, assuming the range was 1-100. We habitually start with half a 100 and then continue to guess half the scope. The process is to constantly divide by 2 until we find the number that we guessed right. The inverse of this process, is constantly multiplied by 2, that is, how many times 2 squared, 2x = 100. Naturally this number of guesses x is the logarithm of base 100 at 2.

To convert from base 2 to base 10 to multiply by 3.322, we can also incorporate this 3.322 constant into K ', thus eliminating the need to specify a base:

T = K ' * LG (N)

For this 3.322 how to come, see the following formula:

Q: What is a power?

**A: A** power that refers to the result of a exponentiation operation. NM refers to n squared m times, the NM as the result of a power, called "N of the M powers".

where n is called "base" and M is called "exponent".

**A:** Master the following operational rules:

1) Multiply with the base power, the base is unchanged, and the exponent is added;

2) Division of the same base power, the base unchanged, exponential subtraction;

3) power, base constant, exponential multiply

**A:** 0 Exponential power

**A:** negative exponential power

**A:** fractional exponential power

Q: What is an exponential function?

**A:** exponential functions (exponential function), generally, the Y=ax function (A is constant and a>0,a≠1) is called an exponential function, and the defined field of the function is R.

**A:** Basic properties:

1) The definition field of the exponential function is R, where a is greater than 0 and not equal to 1. For a is not greater than 0, it is bound to make the definition of the function of the domain is not contiguous, so we do not consider, and a equals 0 function is meaningless generally not considered;

2) The value of the exponential function is (0,+∞);

3) a > 1 o'clock, the exponential function is monotonically increasing; if 0 < a < 1, it is monotonically decreasing;

4) The function always passes (0,1) this;

5) exponential function has inverse function, and its inverse function is logarithmic function;

**A:** the algorithm:

Q: What is a logarithmic function?

**A:** if Ax=n (a > 0, and A≠1), then the number x is called a logarithm of the base N, recorded as X=logan, read as a logarithm of the base N. "Log" is the abbreviation for the Latin logarithm (logarithm).

**A:** Generally, the function Y=logax (a>0, and a≠1) is called the logarithmic function, that is, the power is the independent variable, the exponent is the dependent variable, the base is a constant function, called the logarithmic function.

**A:** Usually we will call the base 10 logarithm common logarithm (common logarithm), and log10n is recorded as LGN.

**A:** in scientific counting, irrational numbers are often used e=2.71828 Logarithm of the base, the logarithm of E is called the natural logarithm (natural logarithm), and the Logen is recorded as a inn.

**A:** Basic properties:

1) The definition field is {x 丨 x > 0}, and the range is the real set R;

2) function image of logarithmic function constant over fixed point (1,0);

3) When a>1, the monotone increment function on the definition field, 0 < A < 1 o'clock, the monotone decrement function on the domain of definition;

**A:** the algorithm:

Q: What are the Big O comparisons of several commonly used algorithms?

**A:** When comparing the algorithm, and do not care about the specific microprocessor chip or compiler, the real need to compare the corresponding to the different n values, so there is no need for constants.

**A:** The large O notation uses the capital letter O, which can be thought of as meaning "order of" about what it means.

The subscript is a summary of the elapsed time of the algorithm we have discussed so far.

**A:** Through it we can compare different large o values: O (1) is excellent, O (Logn) is good, O (N) is also possible, O (N2) is worse.

The essence of the large O notation is not to give actual value to the runtime, but to express how the elapsed time is affected by the number of data items.

Q: Why not use arrays to represent everything?

**A:** just using arrays seems to do all the work, so why not use them to store all the data?

We've seen a lot about the drawbacks of arrays. If the insertion time in an unordered array is fast, but the lookup takes a slower time. It can be found quickly in an ordered array, but it takes a long time to insert. For both arrays, the delete operation time is also slow.

If you have a data structure that takes any action, such as inserting, deleting, and looking up quickly (ideally an O (1) or O (Logn)), that's fine.

Another problem with arrays is that when they are created by new, the size size is fixed. However, it is usually not known how many data items will be put into the array when you start designing the program, so you need to guess its size. If the number of guesses is too large, some elements in the array will never be filled and waste space. If the guess is small, an array overflow occurs.

**A:** There is a vector class in Java that is very much like an array, but it can be extended, and these additional functions are at the expense of efficiency.

You may want to try to create your own vector class. When a class user uses an internal array in the class to overflow, the insertion algorithm creates a larger array, copies the contents of the old array into the new array, and then inserts the new data item. The entire process is not visible to class users.

Q: Reference

- Java data structures and Algorithms Robert Lafore, chapter 2nd-Arrays
- A power in algebra
- exponential function
- Logarithmic functions

Java data structures and algorithms-arrays