How To Add An Index To An Array In Java
An assortment is a collection of items stored at face-to-face memory locations. In this article, we will see how to insert an chemical element in an array in Coffee.
Given an array arr of size n, this article tells how to insert an chemical element 10 in this array arr at a specific position pos.
Approach 1:
Hither'south how to do information technology.
- Commencement get the element to exist inserted, say x
- Then get the position at which this element is to exist inserted, say pos
- Create a new array with the size one greater than the previous size
- Copy all the elements from previous assortment into the new assortment till the position pos
- Insert the element x at position pos
- Insert the balance of the elements from the previous array into the new assortment after the pos
Below is the implementation of the above approach:
Coffee
import
java.io.*;
import
java.lang.*;
import
java.util.*;
class
GFG {
public
static
int
[] insertX(
int
n,
int
arr[],
int
x,
int
pos)
{
int
i;
int
newarr[] =
new
int
[northward +
1
];
for
(i =
0
; i < northward +
ane
; i++) {
if
(i < pos -
1
)
newarr[i] = arr[i];
else
if
(i == pos -
ane
)
newarr[i] = x;
else
newarr[i] = arr[i -
1
];
}
return
newarr;
}
public
static
void
main(Cord[] args)
{
int
n =
10
;
int
i;
int
arr[]
= {
one
,
2
,
three
,
4
,
v
,
6
,
7
,
viii
,
ix
,
10
};
System.out.println(
"Initial Assortment:\n"
+ Arrays.toString(arr));
int
x =
50
;
int
pos =
v
;
arr = insertX(n, arr, 10, pos);
Organisation.out.println(
"\nArray with "
+ x
+
" inserted at position "
+ pos +
":\north"
+ Arrays.toString(arr));
}
}
Output:
Initial Array: [ane, 2, 3, 4, 5, vi, seven, 8, 9, 10] Array with l inserted at position 5: [ane, 2, 3, 4, 50, five, 6, 7, 8, 9, 10]
Approach 2:
Here's how to do information technology.
- Beginning get the element to be inserted, say element
- Then get the position at which this chemical element is to be inserted, say position
- Convert assortment to ArrayList
- Add together element at position using listing.add together(position, element)
- Convert ArrayList back to array and print
Below is the implementation of the higher up approach:
Java
import
coffee.util.ArrayList;
import
coffee.util.Arrays;
import
coffee.util.List;
public
class
AddElementAtPositionInArray {
private
static
void
addElement(
Integer[] arr,
int
chemical element,
int
position)
{
List<Integer> list =
new
ArrayList<>(
Arrays.asList(arr));
list.add(position -
1
, element);
arr = list.toArray(arr);
System.out.println(
"Initial Array:\north"
+ Arrays.toString(arr));
System.out.println(
"\nArray with "
+ chemical element
+
" inserted at position "
+ position +
":\due north"
+ Arrays.toString(arr));
}
public
static
void
main(String[] args)
{
Integer[] arr = {
1
,
2
,
3
,
iv
,
five
,
6
,
7
,
viii
,
9
,
10
};
int
element =
50
;
int
position =
5
;
addElement(arr, element, position);
}
}
Output:
Initial Array: [one, 2, 3, 4, five, 6, seven, eight, 9, ten] Array with l inserted at position v: [ane, ii, 3, iv, 50, 5, half dozen, 7, 8, 9, x]
How To Add An Index To An Array In Java,
Source: https://www.geeksforgeeks.org/how-to-insert-an-element-at-a-specific-position-in-an-array-in-java/
Posted by: huffalhas1974.blogspot.com
0 Response to "How To Add An Index To An Array In Java"
Post a Comment