数组Array
int [ ] nums = new int [ 10 ] ;
int [ ] nums = { { 1 , 2 } , { 3 , 4 } } ;
int n = nums. length;
int [ ] newNums = Arrays . copyOfRange ( nums, 0 , n) ;
Arrays . sort ( nums) ;
字符串String
String str1 = “abc”;
String str2 = new String ( ) ;
char [ ] chs = { 'a' , 'b' , 'c' } ;
String str3 = new String ( chs) ;
String str4 = new String ( chs, 0 , n) ;
int n = str. length ( ) ;
char c = str. charAt ( index) ;
String subStr = str. substring ( index1, index2) ;
boolean falg = str1. equals ( str2) ;
Integer num = Integer . parseInt ( str) ;
String str = String . valueOf ( num) ;
列表List
List < Integer > list = new ArrayList < > ( ) ;
List < List < Integer > > list = new ArrayList < > ( ) ;
List < Integer > list1 = new ArrayList < > ( ) ;
List < Integer > list2 = new ArrayList < > ( ) ;
list. add ( list1) ;
list. add ( list2) ;
list. add ( x) ;
list. add ( index, x) ;
x = list. remove ( index) ;
x = list. get ( index) ;
list. set ( index, x) ;
int n = list. size ( ) ;
list. contains ( x) ;
链表Deque
Deque < Object > deque = new LinkedList < > ( ) ;
boolean flag = deque. isEmpty ( ) ;
deque. addFirst ( x) ;
deque. addLast ( x) ;
deque. removeFirst ( ) ;
deque. removeLast ( ) ;
x = deque. getFirst ( ) ;
x = deque. getLast ( ) ;
哈希表Map
Map < Object , Object > map = new HashMap < > ( ) ;
map. put ( key, value) ;
x = map. get ( key) ;
x = map. getOrDefault ( key, defaultValue) ;
map. put ( key, map. getOrDefault ( key, 0 ) + 1 ) ;
x = map. remove ( key) ;
boolean flag = map. remove ( key, value) ;
boolean flag = map. containsKey ( key) ;
boolean flag = map. containsValue ( value) ;
final boolean [ ] ans = { true }
map. forEach ( ( key, value) -> {
if ( value > 0 ) ans[ 0 ] = false ;
} ) ;
return ans[ 0 ] ;
for ( Map. Entry < Character , Integer > entry : map. entrySet ( ) ) {
if ( entry. getValue ( ) > 0 ) return false ;
}
StringBuilder
StringBuilder sb = new StringBuilder ( ) ;
StringBuilder sb = new StringBuilder ( str) ;
int n = sb. length ( ) ;
sb. append ( "newStr" ) ;
sb. setCharAt ( index, 'x' ) ;
sb. insert ( index, 'x' ) ;
sb. delete ( index1, index2) ;
String str = sb. toString ( ) ;
模板
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner in = new Scanner ( System . in) ;
while ( in. hasNextInt ( ) ) {
int a = in. nextInt ( ) ;
}
String s = in. next ( ) ;
System . out. println ( a) ;
in. close ( ) ;
}
}
链表定义
public class ListNode {
int val;
ListNode next;
public ListNode ( ) {
}
public ListNode ( int val) {
this . val = val;
}
public ListNode ( int val, ListNode next) {
this . val = val;
this . next = next;
}
}