Java library

HashSet

  1. HashSet iterator 变成array
Iterator i=set.iterator();
int[] aim = new int[set.size()];
int j = 0;
    while(i.hasNext()){
        aim[j++]=(int)i.next();
    }

Chacrater

  1. 判断is char 是数字还是字母
Character.isLetter(c) || Character.isDigit(c)

String

  1. int to string
    String.valueOf(int);
    
  2. char to string

    String.valueOf(char);
    
  3. String split

    1. 当引用这个function的时候,要记住如果input是“a b", 那么只会skip一个空格,其他的完全一样。(参考lc151)

The \W+ will match all non-alphabetic characters occurring one or more times. So there is no need to replace. You can check other patterns also. 但是不会开头的空格不会skip掉

        String s = "This?     is a sample sentence with []s.";
        String[] words = s.split("\\W+");
        System.out.println(words[1]);
  1. int to string

    Integer.valueOf()
    
  2. Arrya copy subARRAY

    Arrays.copyOfRange(A, start, end); //不包含end
    
    System.arraycopy(a, start, b, start, number);
    

StringBuilder

  1. sb.append();
  2. sb.deleteCharAt(index);
  3. sb.insert(position, string);
  4. sb.toString();
  5. sb.length()

arraylist to array

String[] stockArr = stock_list.toArray(new String[stock_list.size()]);

res.stream().mapToInt(i->i).toArray()
res.stream().toArray(String[]::new)

List to Set

Set<String> dictSet = new HashSet<>(wordList);

charArray to String

new String(charArray);

results matching ""

    No results matching ""