Home > Software design >  Java duplicate array element at end of print
Java duplicate array element at end of print

Time:02-23

I would like to make a program that receives a user input for an array then removes the first item once and adds it to the end twice returning a new array.

I have utilized the left shift function to complete most of the task but all my program needs now is to add the first item on the list twice at the end.

EDIT: The integer 5 tells the code how many integers will be inputted into the array

My code input:

5
1
2
3
4
7

My code output:

[2, 3, 4, 7, 1]

I would like to get rid of the brackets "[" and the commas leaving spaces in between the number. I want to make the numbers output vertically and I want another one at the end.

Example Input:

5
1
2
3
4
7

Example output:

2
3
4
7
1
1

Code I have now:

import java.util.Arrays;
import java.util.Scanner;
public class Main {

public static int[] shiftLeft(int[] array) {
    //4
    int startNumber = array[0];
    //5
    System.arraycopy(array, 1, array, 0, array.length - 1);
    //6
    array[array.length - 1] = startNumber;
    //7
    return array;
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    //1
    int[] numbers = new int[n];

  for (int i = 0; i < numbers.length; i  ){
    numbers[i] = scan.nextInt();
  }
    
    System.out.println("You have entered : "   Arrays.toString(numbers));

    //3
    int[] finalArray = shiftLeft(numbers);

    //8

    System.out.println("After left shift : "   Arrays.toString(finalArray));  
 }
}

CodePudding user response:

I'm not sure, if you need the shiftLeft as a requirement to solve this problem. But it can be solved simply by using Arrays.copyOf instead of arraycopy. See a sample code below.

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    
    int[] numbers = new int[n-1];
    int first = scan.nextInt();
    for (int i = 0; i < n-1; i  ){
        numbers[i] = scan.nextInt();
    }
    int[] new_arr = Arrays.copyOf(numbers,n 1);
    new_arr[n-1] = first;
    new_arr[n] = first;
    
    for(int num: new_arr)    
        System.out.println(num);
    }
}

Input:

5
1
2
3
4
7

Output:

2
3
4
7
1
1

CodePudding user response:

If I understand what you aim to do...

public static int[] shiftLeft(int[] array) {
    //4
    int startNumber = array[1];
    //5
    System.arraycopy(array, 2, array, 0, array.length - 2);
    //6
    array[array.length - 1] = startNumber;
    array[array.length - 2] = startNumber;
    //7
    return array;
}

First of all, the number you want to move is the SECOND number in the array, not the first (array[1]).

The first element of the array with its size appears to have no purpose, since you can more safely refer to the length using array.length.

You shifted the array to the left by one element. I fixed it so it shifts it to the left by two elements (hence the second argument being 2 as we start copying from the third position of the array, and since it's now a shorter array we also do array.length - 2 in the last arg).

And finally, since we copied one less element, we now need to assign the last two elements to the starting number, not just the last.

CodePudding user response:

So you're really just looking for the right way to print out your array? You could simply

System.out.println("After left shift : ");
for(int i: finalArray) {
    System.out.println(i);
}
  • Related