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 3 of 3

Thread: Arrays not printed? Error?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Arrays not printed? Error?

    Hey guys,
    The following is my code:
    public class OneB {
     
    	/**
    	 * @param args
    	 */
    	public static int[] sumDiffs(int[]a, int[]b){
     
    		int[]sum=new int[a.length];
    		for(int i=0;i<a.length;i++){
    			if(a[i]==b[i]){
    				sum[i]=a[i];
    			}else{
    				sum[i]=a[i]+b[i];
    			}
    		}
    		return sum;
     
    	}
    	public static void main(String[] args) {
    		System.out.println(sumDiffs(new int[] {1,2,3,4,5},new int[]{1,2,3,4,5}));
     
    	}
     
    }

    As you can see from the above code that i have 2 arrays and have to return a new array. For each position at a and b, if the elements at i differ, then the new array should be the sum of the two, otherwise it should be the value shared between the two arrays. The arrays are of the same length.

    But when i try to run the code on eclipse, i get the following error:
    [I@f7e6a96

    whereas it should behave as:
    sumDiffs(new int[] {1,2,3,4,5},new int[]{1,2,3,4,5})-> {1,2,3,4,5}

    Anyone knows where i've gone wrong?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Arrays not printed? Error?

    You are printing what is returned by the method, exactly. Which is not what it seems.
    A quick search reveals this thread

  3. The Following User Says Thank You to jps For This Useful Post:

    hemla (August 4th, 2013)

  4. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Arrays not printed? Error?

    Hello.
    Arrays do not get printed using toString() method.
    Just write an iterative loop to print elements from array one by one.

    Syed.

  5. The Following User Says Thank You to syedbhai For This Useful Post:

    hemla (August 4th, 2013)

Similar Threads

  1. [SOLVED] iam not beeing printed to screen
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2012, 02:06 PM
  2. Need Help! Out of bounds error on ARRAYS!!
    By kram in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2012, 10:02 PM
  3. The positioning and alignment of the text on the paper to be printed
    By java_fledgeling in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 8th, 2010, 08:54 PM
  4. Print Content on Pre Printed Sheet
    By kalees in forum Java Theory & Questions
    Replies: 0
    Last Post: February 24th, 2010, 03:26 AM
  5. instead of printing in the client, report get printed in the server
    By jmvenkat in forum Java Theory & Questions
    Replies: 0
    Last Post: September 19th, 2009, 01:30 AM