상세 컨텐츠

본문 제목

Array and string - Introduction to Dynamic Array from Java to Go

Go/Leet Code

by Gopythor 2022. 3. 23. 15:44

본문

728x90
반응형

Operations in Dynamic Array

https://leetcode.com/explore/learn/card/array-and-string/201/introduction-to-array/1146/

  • In Java it will be called list, in go we call slice.

Java

// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {
        // 1. initialize
        List<Integer> v0 = new ArrayList<>();
        List<Integer> v1;                           // v1 == null
        // 2. cast an array to a vector
        Integer[] a = {0, 1, 2, 3, 4};
        v1 = new ArrayList<>(Arrays.asList(a));
        // 3. make a copy
        List<Integer> v2 = v1;                      // another reference to v1
        List<Integer> v3 = new ArrayList<>(v1);     // make an actual copy of v1
        // 3. get length
        System.out.println("The size of v1 is: " + v1.size());
        // 4. access element
        System.out.println("The first element in v1 is: " + v1.get(0));
        // 5. iterate the vector
        System.out.print("[Version 1] The contents of v1 are:");
        for (int i = 0; i < v1.size(); ++i) {
            System.out.print(" " + v1.get(i));
        }
        System.out.println();
        System.out.print("[Version 2] The contents of v1 are:");
        for (int item : v1) {
            System.out.print(" " + item);
        }
        System.out.println();
        // 6. modify element
        v2.set(0, 5);       // modify v2 will actually modify v1
        System.out.println("The first element in v1 is: " + v1.get(0));
        v3.set(0, -1);
        System.out.println("The first element in v1 is: " + v1.get(0));
        // 7. sort
        Collections.sort(v1);
        // 8. add new element at the end of the vector
        v1.add(-1);
        v1.add(1, 6);
        // 9. delete the last element
        v1.remove(v1.size() - 1);
    }
}

Go

    // 1. initialize
    var v0 = []int{}
    var v1 []int                     // v1 == null
    // 2. cast an array to a vector
    var a = [...]int{0, 1, 2, 3, 4} // Java cannot declare list with int.
    v1 = a[:]                       // Same as b := a[0:len(a)], Reference.

    // 3. make a copy
    var v2 = v1 // another reference to v1
    v3 := make([]int, len(v1))
    copy(v3, v1) // make an actual copy of v1
    fmt.Println(v3)

    // 4. get length
    fmt.Println("The size of v1 is: ", len(v1))
    // 5. access element
    fmt.Println("The first element in v1 is: ", v1[0])
    // 6. iterate the vector
    fmt.Print("[Version 1] The contents of v1 are:")
    for i := 0; i < len(v1); i++ {
        fmt.Print(" ", v1[i])
    }
    fmt.Println()
    fmt.Print("[Version 2] The contents of v1 are:")
    for _, item := range v1 {
        fmt.Print(" ", item)
    }
    fmt.Println()
    // 7. modify element
    v2[0] = 5
    fmt.Println("The first element in v1 is:", v1[0])
    v3[0] = -1
    fmt.Println("The first element in v1 is:", v1[0])

    // 8. sort
    sort.Ints(v1)
    // 9. add new element at the end of the vector
    v1 = append(v1, -1)
    v1 = append(v1, 1, 6)

    // 9. delete the last element
    v1 = v1[:len(v1)-1]

Result

[0 1 2 3 4]
The size of v1 is:  5
The first element in v1 is:  0
[Version 1] The contents of v1 are: 0 1 2 3 4
[Version 2] The contents of v1 are: 0 1 2 3 4
The first element in v1 is: 5
The first element in v1 is: 5
728x90
반응형

관련글 더보기

댓글 영역