Go/Leet Code
Arrays 101 Code from Java to Go(Array Deletions)
Gopythor
2022. 2. 26. 23:27
728x90
반응형
Array Deletions
Deleting From the End of an Array
Code(Creates an Array with room for 10 elements, and then adds elements into the first 6 indexes of it.)
Java
// Declare an integer array of 10 elements.
int[] intArray = new int[10];
// The array currently contains 0 elements
int length = 0;
// Add elements at the first 6 indexes of the Array.
for(int i = 0; i < 6; i++) {
intArray[length] = i;
length++;
}
Go
// Declare an integer array of 10 elements.
var intArray [10]int
// The array currently contains 0 elements
var length int = 0
for i := 0; i < 6; i++ {
intArray[length] = i
length++
}
Code(Deleting the last element of an Array)
Java
// Deletion from the end is as simple as reducing the length
// of the array by 1.
length--;
go
// Deletion from the end is as simple as reducing the length
// of the array by 1.
length--
Code(printArray function)
with length function
Java
for (int i = 0; i < intArray.length; i++) {
System.out.println("Index " + i + " contains " + intArray[i]);
}
*- Print result
Index 0 contains 0.
Index 1 contains 1.
Index 2 contains 2.
Index 3 contains 3.
Index 4 contains 4.
Index 5 contains 5.
Index 6 contains 0.
Index 7 contains 0.
Index 8 contains 0.
Index 9 contains 0.
Go
for i := 0; i < len(intArray); i++ {
fmt.Println("Index", i, "contains", intArray[i])
}
*- Print result
Index 0 contains 0
Index 1 contains 1
Index 2 contains 2
Index 3 contains 3
Index 4 contains 4
Index 5 contains 5
Index 6 contains 0
Index 7 contains 0
Index 8 contains 0
Index 9 contains 0
with length variable
Java
for (int i = 0; i < length; i++) {
System.out.println("Index " + i + " contains " + intArray[i]);
}
*- Print result
before the deletion
Index 0 contains 0.
Index 1 contains 1.
Index 2 contains 2.
Index 3 contains 3.
Index 4 contains 4.
Index 5 contains 5.
After the deletion
Index 0 contains 0.
Index 1 contains 1.
Index 2 contains 2.
Index 3 contains 3.
Index 4 contains 4.
Go
for i := 0; i < length; i++ {
fmt.Println("Index", i, "contains", intArray[i])
}
*- Print result
before the deletion
Index 0 contains 0
Index 1 contains 1
Index 2 contains 2
Index 3 contains 3
Index 4 contains 4
Index 5 contains 5
After the deletion
Index 0 contains 0
Index 1 contains 1
Index 2 contains 2
Index 3 contains 3
Index 4 contains 4
Code(Deleting From the Start of an Array)
Java
// Starting at index 1, we shift each element one position
// to the left.
for (int i = 1; i < length; i++) {
// Shift each element one position to the left
int_array[i - 1] = int_array[i];
}
// Note that it's important to reduce the length of the array by 1.
// Otherwise, we'll lose consistency of the size. This length
// variable is the only thing controlling where new elements might
// get added.
length--;
*- Print result
Index 0 contains 1.
Index 1 contains 2.
Index 2 contains 3.
Index 3 contains 4.
Go
for i := 1; i < length; i++ {
// Shift each element one position to the left
intArray[i-1] = intArray[i]
}
// Note that it's important to reduce the length of the array by 1.
// Otherwise, we'll lose consistency of the size. This length
// variable is the only thing controlling where new elements might
// get added.
length--;
*- Print result
Index 0 contains 1
Index 1 contains 2
Index 2 contains 3
Index 3 contains 4
Code(Deleting From Anywhere in the Array)
Java
// Say we want to delete the element at index 1
for (int i = 2; i < length; i++) {
// Shift each element one position to the left
int_array[i - 1] = int_array[i];
}
// Again, the length needs to be consistent with the current
// state of the array.
length--;
*- Print result
Index 0 contains 1.
Index 1 contains 3.
Index 2 contains 4.
Go
// Say we want to delete the element at index 1
for i := 2; i < length; i++ {
// Shift each element one position to the left
intArray[i - 1] = intArray[i]
}
// Again, the length needs to be consistent with the current
// state of the array.
length--;
*- Print result
Index 0 contains 1
Index 1 contains 3
Index 2 contains 4
728x90
반응형