Is there a way to create an array in java with some initialized numbers and then add random numbers to it

Denard Hyman :

I am trying to create an array with 7 numbers that are manually inputted and 43 that random.

I am able to do it in C but I am facing a challenge in java the 7 numbers that are manually inputted keep coming out as zeros when the array is printed

int arr3[]= {1,2,3,4,5,6,7}; // this is where I created the array 
arr3=new int[50];

// this is where I tried to populate it

for (int i = 7; i < arr5.length; i++) 
{
arr3[i] = (int) (Math.random() * 1500);
} 

I would like the first few numbers to be manually inputted and the rest to be randomly created

Aurelien Montmejat :

It's because you do arr3=new int[50]; you reset every value in the list to 0 and set the size of the array to 50. Here int arr3[]= {1,2,3,4,5,6,7}; you are setting the size to 7. What you could do is this for example :

int arr3[]= new int[50];
arr3[0] = 1;
arr3[1] = 2;
arr3[2] = 3;
arr3[3] = 4;
arr3[4] = 5;
arr3[5] = 6;
arr3[6] = 7;

for (int i = 7; i < arr3.length; i++) {
    arr3[i] = (int) (Math.random() * 1500);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328909&siteId=1