简体   繁体   中英

Printing int[] array from a display method

I am trying to print an int[] array from a seperate method in the same class.

public class LargeInteger {

    public LargeInteger(String s) {

        int[] intArray = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10);
        }
    }

    public Object display() {

         for (int i = 0; i < intArray.length; i++) {     
                System.out.print(intArray[i]);
            }
    }   
}

My intArray is clearly being hidden from the display method, but I am not sure what to do

I will give you the answer but you should first invest some time to look up your problem on google. Google knows "almost" everything...

public class LargeInteger {

    private int[] intArray;

    public LargeInteger(String s) {

        intArray = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10);
        }
    }

    public void display() {

         for (int i = 0; i < intArray.length; i++) {     
                System.out.print(intArray[i]);
            }
    }   
}

And your display method should be void if it isn't returning anything..

intArray is a local variable in the constructor.
It doesn't exist anywhere else.

You need to make a private field instead.

You need to declare the array outside of LargeInteger method, eg

private int[] intArray;

public LargeInteger(String s){

    this.intArray = new int[s.length()];

}
public class LargeInteger {

private int[] intArray;

public LargeInteger(String s) {

    this.intArray = new int[s.length()];

    for (int i = 0; i < s.length(); i++) {
        intArray[i] = Character.digit(s.charAt(i), 10);
    }
}

public Object display() {

     for (int i = 0; i < this.intArray.length; i++) {     
            System.out.print(intArray[i]);
        }
}   
}

Make intArray a member of the LargeInteger class instead of a local to the constructor:

public class LargeInteger {

    private int[] intArray;

    public LargeInteger(String s) {

        intArray = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10);
        }
    }

    public Object display() {

         for (int i = 0; i < intArray.length; i++) {     
                System.out.print(intArray[i]);
            }
    }   
}

Just declare int[] intArray out of the constructor.

It should be

public class LargeInteger {

    private int[] intArray;

    public LargeInteger(String s) {

        intArray = new int[s.length()];

        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10);
        }
    }

    public Object display() {

         for (int i = 0; i < intArray.length; i++) {     
                System.out.print(intArray[i]);
            }
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM