Results 1 to 4 of 4
  1. #1
    blad90 is offline Member
    Join Date
    Aug 2015
    Posts
    3
    Rep Power
    0

    Post Iterate through a loop adding the value of a defined variable

    For example,
    If I have an array:

    A = [2, 1]

    and X = 3

    I want to know how can I iterate through all elements adding the X value to each one to get something like this:

    A = [5, 4]

    (which means the value of X + first element, and the value of X + second element)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Iterate through a loop adding the value of a defined variable

    Are you familiar with for loops?

    Java Code:
    for (int i = 0; i < A.length; i++) {
       A[i] += X;
    }
    Note, variables and instance fields normally start with lowercase letters. Class and interface definitions start with uppercase letters. All of this is by convention. For more Java coding conventions, see the link in my signature. Also, since your question was rather basic, I recommend you go thru the Java tutorials (also linked in my signature).

    Regards,
    Jim
    Last edited by jim829; 09-01-2015 at 03:44 AM. Reason: s/if/is
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Iterate through a loop adding the value of a defined variable

    And this post is supposed to be in New to Java and not in IntelliJ IDEA.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Iterate through a loop adding the value of a defined variable

    Quote Originally Posted by SurfMan View Post
    And this post is supposed to be in New to Java and not in IntelliJ IDEA.
    Consider it done.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2012, 04:29 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 8
    Last Post: 09-12-2011, 10:36 AM
  4. Iterate 2 maps and adding value to each
    By metal_pat in forum New To Java
    Replies: 3
    Last Post: 02-09-2011, 12:23 PM
  5. adding a variable in order to a list
    By Jrr in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 01:10 AM

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
  •