---
# System prepended metadata

title: Java template note

---

# Java template note

* Main func
    ```java
    import java.util.* ; 
    import java.io.*;
    import java.nio.*;

    public class BasicNote{
        public static void main(String[] args){
        }
    }
    ```

* input data
    ```java
    Scanner s = new Scanner(System.in);
    //System.out.println(s.next());
    // hasNextInt , hasNext , nextInt , nextFloat , nextDouble , nextString .... 
    String input = s.nextLine(); 
    s.close();
    ```
        
* string to array 
    ```java
    String[] lines = input.split(" ");
    Vector k = new Vector(Arrays.asList(lines)) ; 
    ```

* convert array to vector 
    ```java
    Integer LengthOfK = lines.length ; 
    ```

* loop , while 
    ```java
    for(Integer i = 0 ; i < lines.length ; i++)
        System.out.println(lines[i]); 
    // get index of array 

    while(LengthOfK!=0){
        LengthOfK-- ; 
        System.out.println(lines[LengthOfK]); 
    }
    ```

* convert string to int , int to string
    ```java
    // convert string to int : Integer.parseInt()  , to string : toString();
    Integer number = Integer.parseInt(lines[0]);
    String S = toString(number) ; 
    ```

* vector
    ```java
    Vector<Integer> v = new Vector<Integer>(); 
    // or Vector v = new Vector(); , it can put in any obj .
    v.setSize(11);
    Collections.fill(v, 0);
    v.get(3) ;              
    // get index of vector
    v.set(3,0) ;   
    // change index of vector
    v.add(new Integer(12));
    v.remove(3);    
    // remove inedex 3
    Collections.sort(v);
    for(Object n:v) System.out.println(n); 
    // Object is a kind of auto in c++ .
    ```
* set
    ```java
    Set<Integer> s = new LinkedHashSet<Integer>(); 
    Set<Integer> s = new HashSet<Integer>(); 
    /*
    LinkedHashSet 保證裡面元素存放的順序與新增時相同
    HashSet 讀取快但不保證存放元素的順序
    查自網路
    */
    s.add(1);
    s.add(2);
    s.add(3);
    //add element
    s.size();
    //return size of set
    s.remove(3);    
    //remove element
    s.contains(2);
    //return True if set contain specific element
    for(Object v:s) System.out.println(v);
    ```
