quinnyb
quinnyb

Reputation: 31

Why is my array not working?

Can someone please help me with this project I need to do

public class ArrayPrinter {

public static void main(String[] args) {
    int[] oneD = { 5, 6, 7, 8 };
    printArray(oneD); 
    System.out.println(); 
    System.out.println("\n"); 

}
public static void printArray(int[][] arr) {
     System.out.print("["); 

     int[][] twoD = {{ 2, 4, 6, 8 },
                    {8, 7, 9, 1},
                    {3, 5, 1, 2}};

     printArray(twoD); 
     System.out.println(); //4
     System.out.println("\n"); 


     int rows = 3;
     int columns = 4;
     int i, j;

     for (i=0; i < rows ; i++) {
         for (j=0; j < columns ; j++) {
             System.out.print( aryNumbers[i] [j] + " ");
         }

        System.out   
     }

     System.out.println("]"); 
}
}

public static final void printArray(int[] arr) {
    System.out.print("["); 
    for (int i = 0; i < arr.length; i++) { 
        if (i == arr.length - 1) 
            System.out.print(arr[i]);
        else
            System.out.print(arr[i] + ", "); 
    }
    System.out.println("]"); 
}

}

nothing is working, and i dont understand why. I have been doing this for 6 hours now and I feel so dumb. I dont get anything at all. Why is the in [][] twoD ={{ 2, 4, 6, 8 }, {8, 7, 9, 1}, {3, 5, 1, 2}}; not posting

Upvotes: 0

Views: 555

Answers (3)

Mr37037
Mr37037

Reputation: 748

First you should know that what is Function Overloading?

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists.

Two functions in a class with same name but different type of parameters or different number of parameters result in function overloading.

For example:

class Test{
    public static void main(String[] args){
        f1(2);//this will print Int function on screen
        f1(2.5);//this will print Double function on screen
    }

    public static void f1(int a){
        //do something
        System.out.println("Int function");
    }
    public static void f1(double a){
        //do something else
        System.out.println("Double function");
    }

}

Similarly in your code you are having two functions with same name

printArray(parameter)

But in both functions parameters are different. One is receiving 1D array and other one is receiving 2D array. So if you want to hit printArray(1D) then just make a 1D array and pass it in parameters. But if you want to hit printArray(2D) then first make a 2D array and then pass that 2D array to printArray function. Then other function will be invoked.

Hope you understand what you are missing.

If you understood what's the problem. Then following is your code in which I have done some modification. But PLEASE first clear the concept and then go to actual coding.

public class Test {

    public static void main(String[] args) {
        int[] oneD = { 5, 6, 7, 8 };
        printArray(oneD); 
        System.out.println(); 
        System.out.println("\n"); 

        int[][] twoD = {{ 2, 4, 6, 8 },
                {8, 7, 9, 1},
                {3, 5, 1, 2}};

        printArray(twoD); 


    }
    public static void printArray(int[][] arr) {
        System.out.println("["); 



        int rows = 3;
        int columns = 4;
        int i, j;

        for (i=0; i < rows ; i++) {
            for (j=0; j < columns ; j++) {
                System.out.print( arr[i] [j] + " ");
            }
            System.out.println();
        }

        System.out.println("]"); 
    }


    public static final void printArray(int[] arr) {
        System.out.print("["); 
        for (int i = 0; i < arr.length; i++) { 
            if (i == arr.length - 1) 
                System.out.print(arr[i]);
            else
                System.out.print(arr[i] + ", "); 
        }
        System.out.println("]"); 
    }

}

Upvotes: 1

rajesh
rajesh

Reputation: 1

First of all your program has some syntax errors and second you have only called 'printArray(oneD);' in the PSVM() method, that's why it's not called the twoD. Please try to correct the program and try again.

Upvotes: 0

Nick Roth
Nick Roth

Reputation: 3077

Looks like you don't ever call the printArray(int[][] arr) method. In main you call printArray(int[] arr) which does its thing then returns. If you don't understand why it's not calling the int[][] version, take some time to learn about method overloading in Java.

Upvotes: 1

Related Questions