# Thread: How do I modify the value of a single element in an array?

1. Member Join Date
Feb 2011
Posts
3
Rep Power
0

## How do I modify the value of a single element in an array?

So i have an array that has a size that is based on the users input (order0) and the cells up to (order0-1) are all randomly generated and the last cell must be a value that when added to the sum of the (order0-1) cells equals a certain number... here is the method i am having trouble with:

public static int[] sumsarray(int s, int o){
int order0=o*2;
int sumall = 0;
int lastsum;
int sumArray[];
int lasti = order0;

//creates the one dimensional array for the sums
sumArray = new int[(order0)];

Random generator = new Random();
for (int i=0; i<(order0-1); i++){
sumArray[i] = generator.nextInt(s/order0);
}

for (int i=0; i<(order0-1); i++){
System.out.println(sumArray[i]);
}
// sums all the arrays up until the last value
for (int i=0; i<(order0-1); i++){
sumall += sumArray[i];
}
System.out.println("the sum of the first sums is:" + sumall);

lastsum = s - sumall;

System.out.println(" the last sum is:" + lastsum);

sumArray[order0]=lastsum;

return sumArray;

my question is how do i get the last sumArray value to be equal to the lastsum. thanks in advance  Reply With Quote

2. Senior Member Join Date
Jun 2008
Posts
2,568
Rep Power
14

## You can use "length" on the array variable (arrayVar.length) which returns the total number of elements in the array. The last array index is one less than that value.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•