Results 1 to 6 of 6
  1. #1
    CaptainBlood is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Red face How do I write a method that returns an array where ith element contains a[i] + b[i]?

    How do I write a method that returns an array where ith element contains a[i] + b[i]?
    This is what I have so far:

    public class NewClass
    {
    public static void main(String[] args)
    {
    int[] res = new int[5];
    int[] arraySum = new int[5];
    int[] a = {1, 2, 3, 4, 5};
    int[] b = {0, 1, 2, 3, 4};

    res = arraySum(a, b);
    System.out.println("The array is: " + res);
    }

    public static int[] arraySum(int[] a, int[] b)
    {
    int[] arr = new int[5];
    for(int i = 0; i < arr.length; i++)
    {
    arr[i] = a[i] + b[i];
    }
    return arr;
    }
    }

    But in the output I get:
    The array is: [I@2e6e1408
    What should I change?
    Thanks ;)

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Change your output statement. You need to run through each part of the array to display it on the screen.

    Java Code:
    for(int i = 0; i < res.length; i++)
         System.out.println(res[i]);
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    CaptainBlood is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by joshdgreen View Post
    Change your output statement. You need to run through each part of the array to display it on the screen.

    Java Code:
    for(int i = 0; i < res.length; i++)
         System.out.println(res[i]);
    Thank you very much, that worked great!

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    No problem, arrays can be tricky at first.
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    To expand on the topic at hand, the reason you get [I@2e6e1408 as an output, is because that is the memory location your array variable references, and it also is the default behavior when trying to print out objects. To avoid these kind of problems in the future with your own classes, always override the toString() method:
    Java Code:
    public class Output1 {
      private int x;
      public Output1(int x) {
        this.x = x;
      }
    }
    
    class Output2 {
      private int x;
      public Output2(int x) {
        this.x = x;
      }
    
      public String toString() {
        return x+""; //the +"" part is to convert x from int to String
      }
    }
    
    class OutputTest {
      public static void main(String[] args) {
        Output1 o1 = new Output1(2);
        Output2 o2 = new Output2(2);
        System.out.println(o1); //here you get something similar as your problem
        System.out.println(o2); //here the output is 2
      }
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    CaptainBlood is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    To expand on the topic at hand, the reason you get [I@2e6e1408 as an output, is because that is the memory location your array variable references, and it also is the default behavior when trying to print out objects. To avoid these kind of problems in the future with your own classes, always override the toString() method:
    Java Code:
    public class Output1 {
      private int x;
      public Output1(int x) {
        this.x = x;
      }
    }
    
    class Output2 {
      private int x;
      public Output2(int x) {
        this.x = x;
      }
    
      public String toString() {
        return x+""; //the +"" part is to convert x from int to String
      }
    }
    
    class OutputTest {
      public static void main(String[] args) {
        Output1 o1 = new Output1(2);
        Output2 o2 = new Output2(2);
        System.out.println(o1); //here you get something similar as your problem
        System.out.println(o2); //here the output is 2
      }
    }
    Thanks, I really appreciate the help.

Similar Threads

  1. Replies: 2
    Last Post: 10-15-2010, 06:09 AM
  2. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  4. Replies: 1
    Last Post: 03-31-2009, 07:40 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 06: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
  •