Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Subtracting each element from an array

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Subtracting each element from an array

    Hi,

    Im looking to find a piece of code which will take an array of ints and then subtract 1 away from each element that array to keep doing this for a certain amount of times. I've managed to make a method which takes away 1 from the array as shown below
    public static void subtraction(int[] num){
    System.out.println("contents of array after subtracting 1 from each element is ");
    for(int i = 0; i < num.length; i++){
    System.out.println((num[i] - 1));

    However I can only get it to subract once, so its obvious to me that I need some form of loop to cycle through and keep subtracting. However every effort Ive made the program will only ouput that first subtraction.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Subtracting each element from an array

    I can only get it to subract once
    Your code shows the contents of the array element minus 1. It does not change the contents of the array.
    You need to use an assignment statement if you want to change the element's value.

Similar Threads

  1. add new element in array
    By allen_k in forum Collections and Generics
    Replies: 3
    Last Post: December 11th, 2011, 03:13 PM
  2. array element declaring problem
    By jack_nutt in forum Collections and Generics
    Replies: 11
    Last Post: August 1st, 2011, 06:29 PM
  3. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 02:26 PM
  4. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM
  5. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 09:04 AM