[java]——Data structure foundation

Preface

Listen to Brother Feng, first use one hour a day to go through the rookie tutorial~ The
road is obstructive and long~

Overview of java data structure

Java Stack class

Stack<Integer> st = new Stack<Integer>();
st.push(new Integer(1)); // 把项压入堆栈顶部。
Integer a = st.peek(); // 查看堆栈顶部的对象,但不从堆栈中移除它。
int place = st.search(1); // 从栈顶往下搜索,返回对象在堆栈中的位置,以 1 为基数。
Integer a = st.pop(); // 移除堆栈顶部的对象,并作为此函数的值返回该对象。
boolean is_empty = st.empty(); // 测试堆栈是否为空。

Java Hashtable 类

Look at it when you use the map

Collection frame

Insert picture description here

Traversal of collection

Traverse the ArrayList

//定义
List<String> list = new ArrayList<String>();
list.add("he")
list.add("she")
//1. 强for:
for(String str:list){
    
    
	System.out.println(str);
}
//2. 集合转换成数组!
String[] strArray = new String[list.size()];
list.toArray(strArray);
for(int i=0;i<strArray.length;i++){
    
    
	System.out.println(strArray[i]);
}
//3. 迭代器
Iterator<String> it = list.iterator();
while(it.hasNext()){
    
    
	System.out.println(it.next());
}

ArrayList

Introduction:

ArrayList inherits AbstractList and implements the List interface

Guide package:

import java.util.ArrayList

initialization:

ArrayList<引用类型> name = new ArrayList<>();

Add elements:

ArrayList<String> list = new ArrayList<String>();
list.add("haha,nihaochun");

Access elements:

list.get(0); // index从0开始

Modify elements:

list.set(0, "haha.nizhendehaochun"); // set(int index, E element) 

Delete elements:

list.remove(0);

Calculate the size:

int sz = list.size();

Iteration:

for(int i = 0;i<list.size();i++){
    
    
	SOUT(list.get(i));
}

Sort 1:

import java.util.Collections;
Collections.sort(list);

Sort 2 (sort the array):

int[] intArray = list.stream().mapToInt(Integer::valueOf).toArray();
Arrays.sort(intArray)

List<Integer>: Converted to int array:

list.stream().mapToInt(Integer::valueOf).toArray();

Other common methods (please add when used):
https://www.runoob.com/java/java-arraylist.html

HashMap

Introduction:

HashMap inherits from AbstractMap and implements Map, Cloneable, and java.io.Serializable interfaces.

Guide package:

import java.util.HashMap;

initialization:

HashMap<Integer, String> Sites = new HashMap<Integer, String>();

Add elements:

Sites.put(1, "Google");

Access element 1:

String s = Sites.get(3);

Access element 2:

String s = Sites.getOrDefault(4, "Noexits!!")

Delete elements:

Sites.remove(4);

Empty:

Sites.clear();

Calculate the size:

Sites.size()

Iteration:

//键
for(Integer i : Sites.keySet()){
    
    
	...
}
//值
for(String s : Sites.values()){
    
    
	...
}
//键-值
for(Integer i : Sites.keySet()){
    
    
	String s = Sites.get(i);
}

Other common methods:
https://www.runoob.com/java/java-hashmap.html

LinkedList

Introduction:
Insert picture description here
Guide package:

import java.util.LinkedList;

initialization:

LinkedList<E> list = new LinkedList<E>();

ArrayList has it, and it will have it. Here are some of its unique and more efficient methods:

Add elements to the head

list.addFirst(...);

Add elements at the end

list.addLast(...);

Remove elements from the head

list.removeFirst();

Remove elements at the tail

list.removeLast();

Get the head element

list.getFirst();

Get the tail element

list.getLast();

Other common methods:
https://www.runoob.com/java/java-linkedlist.html

StringBuffer

Introduction:

However, when the application requires thread safety, you must use the StringBuffer class

initialization:

StringBuffer sb = new StringBuffer("lalala");

increase

sb.append("wwww");

Returns the string representation of the data in this sequence

String ss = sb.toString();

Delete a string in a range

sb.delete(index, index+1);

Delete a single character

sb.deleteCharAt(index); // 得到和上面一样的效果

length

int length = sb.length();

String

Introduction:

Strings are widely used in Java programming. Strings are objects in Java. Java provides the String class to create and manipulate strings.

initialization

String s = "xxxx";

Access a single character

char c = s.charAt(index);

length

int length = s.length();

Other related

Basic type-reference type

We know that the initialization of ArrayList is as follows:

ArrayList<E> list = new ArrayList<>();

E: Generic data type, used to set the data type of objectName, it can only be a reference data type.

In addition, the type stored in HashMap is also a reference type.
So the following describes the common basic types and its reference types:
Insert picture description here

Guess you like

Origin blog.csdn.net/jokerxsy/article/details/114438484