Results 1 to 2 of 2
  1. #1
    OriginalKopy is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Question 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

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. How do I modify the values in an object array?
    By ConMan in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 11:26 PM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM

Posting Permissions

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