Leetcode learn arrays 101 Code from Java to Go(Array Capacity VS Length)

손진성·2022년 2월 10일
0

Leet Code

목록 보기
2/3

How long the DVD is?

Array Capacity

Java

DVD[] array = new DVD[6]

Go

DVDa := [6]int{}			// Array
DVDs := make([]int, 0, 6)	//Slice
  • Declare an integer slice of Capacity 6 and length 0
  • Arrays in Go are static. This means you can store the same data consecutively within a fixed array size.
  • Unlike an array, 'Slice' does not specify a fixed size in advance, and can dynamically change the size as needed afterward, and partial extraction is possible.

Java

int capacity = array.length;
System.out.println("The Array has a capacity of " + capacity);
  • need to access the capacity of an Array by using .length.

Go

var capacity_a int = cap(DVDa)
var capacity_s int = cap(DVDs)

fmt.Println("The Array has a capacity of", capacity_a)
fmt.Println("The Slice has a capacity of", capacity_s)
The Array has a capacity of 6
The Slice has a capacity of 6
  • go.dev/play/p/ltJxpe3JQpt

Array Length

Java

// Create a new array with a capacity of 6.
int[] array = new int[6];

// Current length is 0, because it has 0 elements.
int length = 0;

// Add 3 items into it.
for (int i = 0; i < 3; i++) {
    array[i] = i * i;
    // Each time we add an element, the length goes up by one.
    length++;
}

System.out.println("The Array has a capacity of " + array.length);
System.out.println("The Array has a length of " + length);

Go

//Create a new array with a capacity of 6
var array [6]int
//Create a new array with a capacity of 6
slice := make([]int, 0, 6)

// Current length is 6, even though it has 0 elements.
var length_array = len(array) //6
// Current length is 0, because it has 0 elements.
var length_slice = len(slice) //0

fmt.Println("The Array has a length of", length_array)
fmt.Println("The Slice has a length of", length_slice)
for i := 0; i < 3; i++ {
array[i] = i * i
//slice needs to use append
slice = append(slice, i*i)
// In go, we don't need to make Increment Variables for length.
}

fmt.Println("The Array has a length of", len(array))
fmt.Println("The Slice has a length of", len(slice))
fmt.Println("The Array has a capacity of", cap(array))
fmt.Println("The Slice has a capacity of", cap(slice))
The Array has a length of 6
The Slice has a length of 0
The Array has a length of 6
The Slice has a length of 3
The Array has a capacity of 6
The Slice has a capacity of 6
  • go.dev/play/p/y7wh70WJVep

Handling Array Parameters

Java

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        // Hint: Initialise and declare a variable here to 
        // keep track of how many 1's you've seen in a row.
        for (int i = 0; i < nums.length; i++) {
            // Do something with element nums[i].
        }
    }
}
func findMaxConsecutiveOnes(nums []int) int {
		// Hint: Initialise and declare a variable here to 
		// keep track of how many 1's you've seen in a row.
        for i:=0; i<len(nums); i++ {
		// Do something with element nums[i].
        }
}

Check array and slice in Go

for _, val := range DVDa {
fmt.Println(val)
}
fmt.Println("Capacity", cap(DVDa), "length", len(DVDa), "Array")

for _, val := range DVDs {
fmt.Println(val)
}
fmt.Println("Capacity", cap(DVDs), "length", len(DVDs), "Nil Slice")
0
0
0
0
0
0
Capacity 6 length 6 Array
Capacity 6 length 0 Slice
  • go.dev/play/p/F_IRGim9Byb
profile
Gopyther

0개의 댓글