Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Arrays;
- import java.util.Arrays;
- public class InsertNewValueWherever {
- // metoda przyjmuje nowa wartosc
- // index w ktory ma byc dodana nowa wartosc,
- // tablice oryginalna
- // zwraca nowa tablice
- public static int[] insertNewValue(int newValue, int whatIndex, int[] originArray) {
- int[] destinationArray = new int[originArray.length + 1];
- destinationArray[whatIndex] = newValue;
- for (int i = 0; i < originArray.length; i++) {
- for (int j = 0; j < whatIndex; j++) {
- destinationArray[j] = originArray[j];
- }
- for (int k = whatIndex+1; k < destinationArray.length; k++ ) {
- destinationArray[k] = originArray[k-1];
- }
- }
- System.out.println(Arrays.toString(destinationArray));
- return destinationArray;
- }
- public static void main(String[] args) {
- int[] array = {1, 2, 3, 4};
- insertNewValue(10, 3, array);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement