dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: printing out the return of an array

  1. #1
    Join Date
    Jan 2018
    Location
    Java 8
    Posts
    16

    printing out the return of an array

    I am trying to print out the return of my method but i keep getting something different. The problem states:

    Implement a method named totals that takes a two dimensional integer array named array as a parameter
    and returns a one dimensional array where each element is the sum of the columns of the input array.

    the method that i wrote is:

    Code:
    public class ArrayMethods2 {
    public int[] totals (int[][] array){
    		int[] sumCol = new int[array[0].length];
    		
    		for(int row = 0; row < array.length; row++){
    			for(int col = 0; col < array[row].length; col++){
    				sumCol[col] += array[row][col];
    			}
    		}
    		return sumCol;
    	}
    and my main method to run it i have:

    Code:
    import java.util.Scanner;
    
    public class RunArray2{
    
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		ArrayMethods2 am2 = new ArrayMethods2();
    
                    System.out.println("\nMethod \"totals"+ "\"");
    		System.out.println("Enter dimension of two dimensonal array");
    		System.out.print("row size: ");
    		int row = scan.nextInt();
    		
    		System.out.print("col size: ");
    		int col = scan.nextInt();
    		
    		int[][] twoDArray = new int[row][col];
    		
    		for(int i=0; i < twoDArray.length; i++){
    			for(int j=0; j < twoDArray[i].length; j++){
    				twoDArray[i][j] = scan.nextInt();
    			}
    		}
    		
    		System.out.println("The array entered is: ");
    		for(int i=0; i < twoDArray.length; i++){
    			for(int j=0; j < twoDArray[i].length; j++)
    				System.out.print(twoDArray[i][j] + " ");
    			System.out.println();
    		}
    		
    		System.out.println("Sum of the columns: ");
    		System.out.println(am2.totals(twoDArray));
    	}
    }
    my output is this:

    Code:
    Method "totals"
    Enter dimension of two dimensonal array
    row size: 3
    col size: 3
    1 2 3
    4 5 6
    7 8 9
    The array entered is: 
    1 2 3 
    4 5 6 
    7 8 9 
    Sum of the columns: 
    [I@3d4eac69
    trying to figure out why i am getting that for the sum of my columns. any suggestions?
    Last edited by rcheeks23; February 25th, 2018 at 07:42 PM.

  2. #2
    Join Date
    Jan 2018
    Location
    Java 8
    Posts
    16

    Re: printing out the return of an array

    so i figured it out. After walking away from it. wasnt sure how to delete the post but i changed the part where i print out and call the method a little by doing this.

    Code:
    		int[] sum = am2.totals(twoDArray);
    		System.out.println("Sum of the columns: ");
    		System.out.println(Arrays.toString(sum));

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)