Advertisement
hackmate

Untitled

May 29th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. package Arrays;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class InsertNewValueWherever {
  6.  
  7. // metoda przyjmuje nowa wartosc
  8. // index w ktory ma byc dodana nowa wartosc,
  9. // tablice oryginalna
  10. // zwraca nowa tablice
  11.  
  12. public static int[] insertNewValue(int newValue, int whatIndex, int[] originArray) {
  13. int[] destinationArray = new int[originArray.length + 1];
  14. destinationArray[whatIndex] = newValue;
  15. for (int i = 0; i < originArray.length; i++) {
  16.  
  17. for (int j = 0; j < whatIndex; j++) {
  18. destinationArray[j] = originArray[j];
  19. }
  20.  
  21. for (int k = whatIndex+1; k < destinationArray.length; k++ ) {
  22. destinationArray[k] = originArray[k-1];
  23. }
  24. }
  25. System.out.println(Arrays.toString(destinationArray));
  26. return destinationArray;
  27. }
  28.  
  29. public static void main(String[] args) {
  30. int[] array = {1, 2, 3, 4};
  31. insertNewValue(10, 3, array);
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement