// 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++;
}
// 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++
}
// Deletion from the end is as simple as reducing the length
// of the array by 1.
length--;
// Deletion from the end is as simple as reducing the length
// of the array by 1.
length--
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.
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
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.
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
// 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.
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
// 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.
// 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
Array101 Remove Duplicates from Sorted Array in GO (0) | 2022.03.01 |
---|---|
Leetcode Array101 Remove Element in GO (0) | 2022.02.27 |
Array101 Merge Sorted Array in GO (0) | 2022.02.25 |
Switch for merge array in Go (0) | 2022.02.24 |
Quick sort with merge array in Go (0) | 2022.02.24 |
댓글 영역