Java - Basics

Explanation of Terms

  • In-Place Algorithm(原地算法): an in-place algorithm is an algorithm which transforms input using no auxiliary data structure. However a small amount of extra storage space is allowed for auxiliary variables. The input is usually overwritten by the output as the algorithm executes. In-place algorithm updates input sequence only through replacement or swapping of elements. (Wikipedia)

Basic Knowledge

Character Class

  1. https://www.tutorialspoint.co...
  2. https://docs.oracle.com/javas...

Integer to String

  1. Convert using Integer.toString(int)
  2. Convert using String.valueOf(int)

To Declare an ArrayList with Values

For examplehtml

List<String> temp = new ArrayList<String>(Arrays.asList("1", "12"));

Iterate through a HashMap

https://stackoverflow.com/que...java

If you're only interested in the keys, you can iterate through the keySet() of the map:算法

Map<Object, Object> map = ...;

for (Object key : map.keySet()) {
    // ...
}

If you only need the values, use values():api

for (Object value : map.values()) {
    // ...
}

Finally, if you want both the key and value, use entrySet():oracle

for (Map.Entry<Object, Object> entry : map.entrySet()) {
    Object key = entry.getKey();
    Object value = entry.getValue();
    // ...
}

Coding Test

i++ & ++i 賦值

public class test {

    public static void main(String[] args) {    
        
        int i1=0, i2=0, i3=0, i4=0, index1=0, index2=0, index3=0, index4=0;
        int[] num1 = new int[10];
        int[] num2 = new int[10];
        int[] num3 = new int[10];
        int[] num4 = new int[10];
        while (index1 < 5 && index2 < 5 && index3 < 5 && index4 < 5) {
            num1[index1++] = i1++;       //01234  
            num2[++index2] = i2++;       //001234  
            num3[index3++] = ++i3;       //12345
            num4[++index4] = ++i4;       //012345
        }
        for (int n : num1) {
            System.out.print(n);
        }
        System.out.println();
        for (int n : num2) {
            System.out.print(n);
        }
        System.out.println();
        for (int n : num3) {
            System.out.print(n);
        }
        System.out.println();
        for (int n : num4) {
            System.out.print(n);
        }
    }
}

/* Output
 * 0123400000
 * 0012340000
 * 1234500000
 * 0123450000
*/

要寫的

  1. StringBuilder & StringBuffer
  2. Stack & Queue
  3. Hash Table & HashMap & HashSet
  4. Iterator
相關文章
相關標籤/搜索