Leetcode learn array Code from Java to Go(Insert)

손진성·2022년 2월 21일
0

Inserting at the End of an Array

Java

// Declare an integer array of 6 elements
int[] intArray = new int[6];
int length = 0;

// Add 3 elements to the Array
for (int i = 0; i < 3; i++) {
    intArray[length] = i;
    length++;
}

Go

// Declare an integer array of 6 elements
var intArray [6]int
var length int = 0

// Add 3 elements to the Array
for i:=0; i<3; i++{
	intArray[length] = i
	length++
	}
}

printArray

Java

for (int i = 0; i < intArray.length; i++) {
    System.out.println("Index " + i + " contains " + intArray[i]);
}

Go

for i := 0; i < len(intArray); i++ {
	fmt.Println("Index", i, "Contains", intArray[i])
}
  • go.dev/play/p/dgUwKFJ0u5f

Go

Index 0 Contains 0
Index 1 Contains 1
Index 2 Contains 2
Index 3 Contains 0
Index 4 Contains 0
Index 5 Contains 0

Add a 4th element

Java

// Insert a new element at the end of the Array. Again,
// it's important to ensure that there is enough space
// in the array for inserting a new element.
intArray[length] = 10;
length++;

Go

// Insert a new element at the end of the Array. Again,
// it's important to ensure that there is enough space
// in the array for inserting a new element.
intArray[length] = 10
length++

Go

Index 0 Contains 0
Index 1 Contains 1
Index 2 Contains 2
Index 3 Contains 10
Index 4 Contains 0
Index 5 Contains 0

Inserting at the Start of an Array

Java

// First, we will have to create space for a new element.
// We do that by shifting each element one index to the right.
// This will firstly move the element at index 3, then 2, then 1, then finally 0.
// We need to go backwards to avoid overwriting any elements.
for (int i = 3; i >= 0; i--) {
    intArray[i + 1] = intArray[i];
}

// Now that we have created space for the new element,
// we can insert it at the beginning.
intArray[0] = 20;

Go

// First, we will have to create space for a new element.
// We do that by shifting each element one index to the right.
// This will firstly move the element at index 3, then 2, then 1, then finally 0.
// We need to go backwards to avoid overwriting any elements.
for i := 3; i >= 0; i-- {
	intArray[i+1] = intArray[i]
}

// Now that we have created space for the new element,
// we can insert it at the beginning.
intArray[0] = 20

Go

Index 0 Contains 20
Index 1 Contains 0
Index 2 Contains 1
Index 3 Contains 2
Index 4 Contains 10
Index 5 Contains 0
  • go.dev/play/p/CKrjJR9n233

Inserting Anywhere in the Array

Java

// Say we want to insert the element at index 2.
// First, we will have to create space for the new element.
for (int i = 4; i >= 2; i--)
{
    // Shift each element one position to the right.
    intArray[i + 1] = intArray[i];
}

// Now that we have created space for the new element,
// we can insert it at the required index.
intArray[2] = 30;
// Say we want to insert the element at index 2.
// First, we will have to create space for the new element.
for i := 4; i >= 2; i-- {
	// Shift each element one position to the right.
	intArray[i+1] = intArray[i]
}
// Now that we have created space for the new element,
// we can insert it at the required index.
intArray[2] = 30

Go

Index 0 Contains 20
Index 1 Contains 0
Index 2 Contains 30
Index 3 Contains 1
Index 4 Contains 2
Index 5 Contains 10
  • go.dev/play/p/CeElLOqrIE8
profile
Gopyther

0개의 댓글