Results 1 to 8 of 8
  1. #1
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default how do you add up integers in an array?

    seems like a simple question but for some reason it's avoiding me :/

    say in a situation where you are given the array like

    public int addArray ( int[] array ) {

    and in the array was something like "addArray({1,2,3});" but it was not known.

    how would you return 6?

    many thanks for your replies :)

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    You need a variable that will keep the total.
    Then you need a for..loop that will go through the array and add the value held to the total.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by shazakala View Post
    seems like a simple question but for some reason it's avoiding me :/

    say in a situation where you are given the array like

    public int addArray ( int[] array ) {

    and in the array was something like "addArray({1,2,3});" but it was not known.

    how would you return 6?

    many thanks for your replies :)
    That is not valid Java syntax, i.e. the compiler can't tell what the type of { 1, 2, 3 } is; it could be an int array but it could also be a char array or a byte array, or a long array ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by JosAH View Post
    That is not valid Java syntax, i.e. the compiler can't tell what the type of { 1, 2, 3 } is; it could be an int array but it could also be a char array or a byte array, or a long array

    but if the signature of the method is addArray ( int[] array ) you will able to pass only ints. anyway.

    @shazakala: assuming the programmer pass only ints array then inside the methods you could use a for-loop with (int i=0; i < array.length; i) to iterate thorugh the array and sum the numbers and return the sum value.
    Last edited by j2me64; 04-19-2011 at 09:56 AM.

  5. #5
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    thanks j2me64 :D solved

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    but if the signature of the method is addArray ( int[] array ) you will able to pass only ints. anyway.
    Java doesn't work that way; it can't cast arrays because it has to cast each element in the array and create a new array in the worst case; e.g. { 1L, 2.0, '3' }, can it be an actual parameter value for an int[] type parameter?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    thats not the point, it was an example, all you need is the gist, and j2 had it and helped XD

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by shazakala View Post
    thats not the point, it was an example, all you need is the gist, and j2 had it and helped XD
    Oh well, as long as you've realized that the example was utterly incorrect.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Printing repeating integers in a given array?
    By baumboards in forum New To Java
    Replies: 7
    Last Post: 01-24-2011, 07:08 PM
  2. Replies: 7
    Last Post: 02-04-2010, 11:09 AM
  3. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 03:44 PM
  4. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 08:58 AM
  5. Replies: 2
    Last Post: 04-16-2008, 12:35 PM

Tags for this Thread

Posting Permissions

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