JavaSprinter JavaSprinter - 1 year ago 71
Java Question

If I have an array of 5 elements lets say (1,2,3,4,5) how do create a new one with double each integer?

If I have an array of of elements how do I create one a new array of the same size where each integer in the array is twice the value then in the previous array - i.e. old array { 1 , 2 , 3 } new = { 4 , 5 ,6 }

The program below takes in an integer between 3 and 10 and outputs an array starting at 1 upto the element they put in so if they enter 5 an array is created that is {1,2,3,4,5}

I'm not sure how to create a new array from an old one.

public class Arraya {
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.print("Enter a number of rows between 3 and 10 ");

Scanner rowScan = new Scanner(;
int rowInput = rowScan.nextInt();
int[] numberArray = new int[rowInput];
for(int i =0;i< rowInput;i++)
{ numberArray[i] = i+1 ;



Answer Source

if you want values in a new array, then

int[] array ={1,2,3,4,5};
int arrayLength = array.size();
int[] array2 = new int[arrayLength];
for(int i=0; i<arrayLength; i++)
    array2[i] = array[i]*2;

If you want values doubles in the same array, then

int[] array ={1,2,3,4,5};
for(int i=0; i<array.size(); i++)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download