In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you how java custom class encapsulation array to achieve data operation related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.
As a basic data storage structure, array is widely used. An array is a data structure that uses continuous memory space to store fixed length and the same data type. The data structure is language-independent. Here, java is used for array-related operations. The index of the array starts at 0.
Array initialization
There are two ways to create data, one is to declare a fixed-length data, and then to assign values to the array, and the other is to assign values directly.
The first kind:
Data type [] array name = new data type [length]
The [] flag here declares an array, which can be placed not only after the data type, but also after the array noun, with the same effect. Suppose I declare an array of type long of length 2 and assign a value:
Long [] arr = new long [2]; arr [0] = 1 Tesar [1] = 2
The second kind:
Data type [] array name = {element 1, element 2,.}
In this way, the array is assigned directly when the array is initialized, and the length of the array is determined by the number of elements.
Two custom classes encapsulate arrays to implement data operations public class MyArray {/ / custom array private long [] arr; / / effective data length private int element; public MyArray () {arr = new long [9];} public MyArray (int maxsize) {arr = new long [maxsize] } / * display array elements * / public void display () {System.out.print ("["); for (int I = 0; I
< element; i++) { System.out.print(arr[i]+" "); } System.out.print("]"); }}2.1 添加元素 数组是用连续的内存空间来存储数据的,则每次添加的时候会往当前数组的最后一个元素上添加元素,一次就可以加上元素,所以它的复杂度为O(1),假如定义一个长度为9数组,数组中已经有两个元素,则添加第三个元素如下:Public void add (long value) {arr [element] = value; element++;} 2.2 query element location by value
This kind of search method is also called linear search, which is to cycle through the element according to the passed value to get the corresponding position. theoretically, it takes an average of 2 times to query an element, so its complexity is O (N).
Public int find (long value) {int i; for (I = 0; I)
< element; i++) { if(value == arr[i]){ break; } } if(i == element){ return -1; }else { return i; }}2.3 根据索引查询元素 根据索引来查找元素,也就是获取对应位置的元素,其复杂度为O(1)。 public long get(int index){ if(index >= element | | index
< 0){ throw new ArrayIndexOutOfBoundsException(); }else { return arr[index]; }}2.4 根据索引删除元素 删除对应索引的元素后,我们需要将所有改索引后面的元素,向前移动一位。假如我要删除索引为2的元素,如下: 理论上平均删除一个元素,我们需要移动N/2次,所以它的时间复杂度也为O(N)。 public void delete(int index){ if(index >= element | | index
< 0){ throw new ArrayIndexOutOfBoundsException(); }else { for (int i = index; i < element; i++) { arr[index] = arr[index+1]; } element --; }}2.5 修改元素 修改某个位置的元素,直接根据索引就一次就可以修改对应的元素,所以它的时间复杂度为O(1)。 public void change(int index,long newValue){ if(index >= element | | index
< 0){ throw new ArrayIndexOutOfBoundsException(); }else { arr[index] = newValue; }}三 有序数组 有序数组是数组的一种特殊类型,有序数组中的元素按照某种顺序进行排列。 3.1 添加元素 在添加元素的时候,将元素按顺序添加到某个位置。如下,在一个数组中添加一个33的元素。First, move the element with index 3 to the position with index 4, then move the element with index 2 to the position with index 3, and finally add 33 to the position with index 2. Theoretically, inserting an element requires 2 moving elements, so its time complexity is O (N).
Public void add (long value) {int i; for (I = 0; I)
< element; i++) { if(arr[i]>Value) {break;}} for (int j = element; j > I; JMY -) {arr [j] = ARR [j-1];} arr [I] = value; element++;} 3.2 binary method to query the index based on elements
In an unordered array, the linear method is used to find the relevant elements, that is, by index. Ordered arrays can use dichotomy to find elements, which means dividing an array into two in the middle, determining which array the element is in, and then repeating the operation.
If you have an array of eight elements, and the content of the array is an ordered sequence of 0-7, look for the element 5, divide it into 0-3 and 4-7 for the first time, and then divide 4-7 into 4-5 and 6-7. Finally, you can query out the specific elements by dividing 4-5 into 4 and 5, so that you can query the specific elements in the array with length 8 for 3 times. Its complexity is O (logN) (the base number of logN in a computer generally refers to 2, meaning that the power of 2 equals n).
Public int search (long value) {/ / Intermediate value int middle = 0; / / minimum value int low = 0; / / maximum int pow = element; while (true) {middle = (low + pow) / 2; if (arr = = value) {return middle;} else if (low > pow) {return-1 } else {if (arr > value) {pow = middle-1;} else {low = middle + 1;} four summaries
Lower complexity means better algorithms, so O (1) > O (logN) > O (N) > O (N ^ 2).
Algorithm complexity linear search O (N) dichotomy search O (logN) unordered array insert O (1) ordered array insert O (N) unordered array delete O (N) ordered array delete O (N)
Unordered array insert fast, find and delete slow
Ordered array lookup is fast, insert and delete is slow
These are all the contents of the article "how to customize java classes to encapsulate arrays to achieve data manipulation". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.