You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Implement deletion from array by shifting elements left
🧠 Logic:
- Deletes an element at a specific index by shifting all subsequent elements one position to the left.
- Reduces the logical size of the array to reflect the deletion.
- Demonstrates a basic array deletion algorithm without using built-in methods.
Signed-off-by: Somesh diwan <[email protected]>
Merging two 1D arrays involves combining the elements of both arrays into a single array while maintaining their original order.
2
+
Merging 2 arrays:
3
+
Merging two 1D arrays involves combining the elements of both arrays into a single array while maintaining
4
+
their original order.
4
5
5
6
Here's a step-by-step process for merging two 1D arrays:
6
7
7
-
Create a New Array: Create a new array that is large enough to hold the combined elements of both arrays. The size of the new array should be the sum of the sizes of the two original arrays.
8
-
Copy Elements: Iterate through the elements of the first array and copy them to the new array. Then, iterate through the elements of the second array and copy them to the new array after the elements of the first array.
8
+
Create a New Array: Create a new array that is large enough to hold the combined elements of both arrays.
9
+
The size of the new array should be the sum of the sizes of the two original arrays.
10
+
Copy Elements: Iterate through the elements of the first array and copy them to the new array.
11
+
12
+
Then, iterate through the elements of the second array and copy them to the new array after the elements of the first array.
9
13
Result: The new array now contains all the elements from both original arrays, merged in the desired order.
10
-
Task
14
+
11
15
You are given a template code in the IDE.
12
-
Update the code to merge the 2 arrays based on the process defined above.*/
16
+
Update the code to merge the 2 arrays based on the process defined above.
17
+
*/
13
18
14
19
publicclassArrayIMPCC2{
15
20
publicstaticvoidmain(String[] args) {
@@ -26,51 +31,53 @@ public static void main(String[] args) {
26
31
secondArray[1] = 10;
27
32
secondArray[2] = 12;
28
33
secondArray[3] = 14;
29
-
intsize2 = 4;
30
34
31
-
// Update the code below to solve the problem.
35
+
intsize2 = 4;
32
36
33
37
intmergedSize = size1 + size2;
34
38
35
39
intmergedArray[] = newint[mergedSize];
36
40
37
-
for(inti = 0; i < size1; i++)
38
-
{
41
+
for(inti = 0; i < size1; i++) {
39
42
mergedArray[i] = firstArray[i];
40
43
}
41
44
42
-
for(inti = 0; i<size2; i++)
43
-
{
45
+
for(inti = 0; i<size2; i++) {
44
46
mergedArray[size1+i] = secondArray[i];
45
47
}
46
48
47
-
for(inti = 0; i < mergedSize; i++)
48
-
{
49
+
for(inti = 0; i < mergedSize; i++) {
49
50
System.out.print(mergedArray[i] + " ");
50
51
}
51
52
}
52
53
}
53
54
54
55
/*
55
-
If you directly print the merged array without using a for loop, Java will print the array's memory address (hash code) instead of its actual elements. This happens because arrays in Java are objects, and the default toString() method for arrays doesn't override the Object class's toString() method.
56
+
If you directly print the merged array without using a for loop,
57
+
Java will print the array's memory address (hash code) instead of its actual elements.
58
+
59
+
This happens because arrays in Java are objects, and the default toString() method for arrays doesn't
60
+
override the Object class's toString() method.
56
61
57
-
For example:
62
+
For example:
58
63
59
-
System.out.println(mergedArray);
60
-
This will output something like: [I@1b6d3586
64
+
System.out.println(mergedArray);
65
+
This will output something like: [I@1b6d3586
61
66
62
-
This is not useful for understanding the contents of the array. To print the actual elements of the array, you must use a loop or a method like Arrays.toString().
67
+
This is not useful for understanding the contents of the array.
68
+
To print the actual elements of the array, you must use a loop or a method like Arrays.toString().
63
69
64
-
Correct Way to Print Without a Loop
65
-
If you want to avoid writing a for loop, you can use Arrays.toString() from the java.util.Arrays class:
70
+
Correct Way to Print Without a Loop
71
+
If you want to avoid writing a for loop, you can use Arrays.toString() from the java.util.Arrays class:
66
72
67
-
import java.util.Arrays;
73
+
import java.util.Arrays;
74
+
System.out.println(Arrays.toString(mergedArray));
75
+
This will print the array in a readable format,
68
76
69
-
System.out.println(Arrays.toString(mergedArray));
70
-
This will print the array in a readable format, like:
77
+
like:
78
+
[2, 4, 6, 8, 10, 12, 14]
71
79
72
-
[2, 4, 6, 8, 10, 12, 14]
73
-
Key Takeaway
74
-
Directly printing an array will not show its elements.
75
-
Use Arrays.toString() for a quick and clean way to print the array without writing a loop.
80
+
Key Takeaway
81
+
Directly printing an array will not show its elements.
82
+
Use Arrays.toString() for a quick and clean way to print the array without writing a loop.
0 commit comments