Data structure learning (Java version) - Array

Ordinary arrays

Array definition:

An array is an ordered collection of the same type. An array of the same type described in a number of data, according to certain combination order in which they. Wherein each array element is called a data, each array element can be accessed through an index .

The basic features of the array:

  1. Its length is determined on the creation, and can not be modified.
  2. Its elements must be the same type.
  3. Which may be any data elements, and type of medical type comprising a base.
  4. Indexed arrays can have meaning, for example can learn numbers.

Create and initialize an array of

Declare an array

// 阿里巴巴强制规范
type[] arr_name;


type arr_name[];

Array initialization

1. static initialization

int[] a = {1, 2, 3};

Man[] mans = {
new Man(1, 1);
new Man(2, 2);
};

2. Dynamic initialization

int[] a = new int[2];
a[0] = 1;
a[1] = 2;

3. The array of default initialization

// {0, 0}
int[] a = new int[2];
// {false, false}
boolean[] b = new boolean[2];
// {null, null}
String[] s = new String[2];

4. The array bounds

Subscripts legitimate interval [0, length-1]

Dynamic Array

Dynamic array definition:

    ArrayList in Java is dynamic arrays, and provides increased dynamic element reduction.

The basic characteristics of a dynamic array:

  1. Array size can be automatically adjusted according to the number of elements.
  2. You can not store the basic data types.

Create and initialize a dynamic array

Declare a dynamic array initialization

ArrayList<type> arr_name = new ArrayList<>();

The bounds of the array: subscripts legal interval [0, size-1]

Summary:

boolean add(E e)
          The specified element to the end of this list.
 void add(int index, E element)
          The specified element into the specified position in this list.
 boolean addAll(Collection<? extends E> c)
          In accordance with the order of the elements of the specified collection iterator returned, all the elements of the collection to the tail of this list.
 boolean addAll(int index, Collection<? extends E> c)
          Starting at the specified location, all of the elements in the specified collection into this list.
 void clear()
          Remove all the elements in this list.
 Object clone()
          Returns a shallow copy of this ArrayList instance.
 boolean contains(Object o)
          If you specify the elements included in this list, it returns true.
 void ensureCapacity(int minCapacity)
          If necessary, to increase the capacity of this ArrayList instance, to ensure that it can hold at least the minimum capacity of the number of elements specified by the parameters.
 E get(int index)
          Returns the specified element in this list on location.
 int indexOf(Object o)
          Returns the index of the specified element in this list for the first time or if this list does not contain the element, or -1.
 boolean isEmpty()
          If no elements in this list are true
 int lastIndexOf(Object o)
          Returns the index of the specified element in this list of the last occurrence, or if this list does not contain an index, or -1.
 E remove(int index)
          Removes the element at the location specified in this list.
 boolean remove(Object o)
          Removes the specified element in this list of the first occurrence (if present).
protected  void removeRange(int fromIndex, int toIndex)
          Remove all of the elements in the index list between fromIndex (inclusive) and toIndex (not included) of.
 E set(int index, E element)
          Alternate with the specified elements in this list element at the specified position.
 int size()
          Returns the number of elements in this list.
 Object[] toArray()
          Proper sequence array containing all of the elements in this list (from the first to the last element).
<T> T[]
toArray(T[] a)
          Appropriate order (from the first to the last element) Returns an array of all elements in the list; when returned array of the specified type is the type of array operation.
 void trimToSize()
          ArrayList instance of this capacity adjustment of the size of the current list.

 

example:

Array: stay button - question number -1313- https://leetcode-cn.com/problems/decompress-run-length-encoded-list/submissions/

 

To give you a run-length encoding compression list of integers nums.

Consider two elements of each pair of adjacent [a, b] = [nums [2 * i], nums [2 * i + 1]] (where i> = 0), each pair expressed a rear decompression value of b elements.

Please return to the list after decompression.

Example:

Input: nums = [1,2,3,4]
Output: [2,4,4,4]
Explanation: a first pair [1,2] represents the frequency of occurrence 2 is 1, to produce the array [2].
The second pair [3,4] represents the frequency of occurrence 4 is 3, to produce the array [4,4,4].
Finally together in series to [2] + [4,4,4,4] = [2,4,4,4].

prompt:

2 <= nums.length <= 100
nums.length % 2 == 0
1 <= nums[i] <= 100

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/decompress-run-length-encoded-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

(这里注意下,实际上这种方式很麻烦,只是为了写一个数组应用的例子)

class Solution {
    public int[] decompressRLElist(int[] nums) {
		int size = 0;
		for (int i = 0; i < nums.length; i+=2) {
			size+=nums[i];
		}
		int[] res = new int[size];
		int pointer = 0;
		for (int i = 0; i < res.length; i+=2) {
			if(i<nums.length) {
				for (int j = 0; j < nums[i]; j++) {
					res[pointer] = nums[i+1];
					pointer++;
				}
			}
		}
        return res;
    }
}

动态数组

class Solution {
    public int[] decompressRLElist(int[] nums) {
		ArrayList<Integer> a = new ArrayList<>();
		for (int i = 0; i < nums.length; i+=2) {
			for (int j = 0; j < nums[i]; j++) {
				a.add(nums[i+1]);
			}
		}
		int[] res = new int[a.size()];
		for (int i = 0; i < a.size(); i++) {
			res[i] = a.get(i);
		}
        return res;
    }
}

 

 
发布了33 篇原创文章 · 获赞 22 · 访问量 6737

Guess you like

Origin blog.csdn.net/qq_42909053/article/details/104301726