【问题标题】:Why can't I an array in a method outside its class?为什么我不能在其类之外的方法中使用数组?
【发布时间】:2015-05-01 10:26:19
【问题描述】:

我已经创建了数组,并且在它的类之外我创建了一个对数组进行排序的方法。它一直说找不到我制作的数组的变量名。当我采用该方法并将其放入与数组相同的类中时,它可以工作,但它违背了我想要实现的目的,帮助?

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {
        // TODO code application logic here

        System.out.println("Enter a length for the array: ");
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();

        int randomNumbers[] = new int[x];

        for (int index = 0; index < randomNumbers.length; index++)

        {
            randomNumbers[index] = (int) (Math.random() * 100);
        }

        for (int index = 0; index < randomNumbers.length; index++)

        {
            System.out.println(randomNumbers[index]);
        }

    }

    static void sortAscending()

    {
        Arrays.sort(randomNumbers);

        for (int i = 1; i < randomNumbers.length; i++) {
            System.out.println("Number: " + randomNumbers[i]);
        }
    }  

【问题讨论】:

  • randomNumbers 作为方法参数传递。如果您需要返回排序后的array 而不仅仅是打印内容,return 它。
  • 我正在解决一个问题,要求我编写一个方法 sortAscending 和一个降序方法,我已经创建了它们,但是我将如何返回数组而不是打印。
  • 糟糕的代码格式。如果您希望有人帮助您,请努力正确地提出您的问题(格式等)

标签: java arrays sorting methods syntax


【解决方案1】:

由于randomNumbers是在main方法中声明的,其他方法无法访问。有几种方法可以使数组可以从其他方法访问,例如:

  1. 作为参数传递给方法:

    static void sortAscending(int[] randomNumbers) {
        //...
    }
    

    然后像这样从main 拨打sortAscending 电话

    sortAscending(randomNumbers);
    
  2. 通过字段传递值。但是,我不会使用静态字段,因为所有实例只有这些字段之一。但是您可以使用您的类的实例并将值存储在非静态字段中:

    publc class MyClass {
    
        // declare randomNumbers as field
        private int[] randomNumbers;
    
        public static void main(String[] args) {
            MyClass o = new MyClass();
            o.localMain(args);
    
            // you could call sortAscending here like this
            o.sortAscending();
        }
    
        // you don't really need to pass args, since you don't use it
        public void localMain(String[] args) {
            // TODO code application logic here
    
            System.out.println("Enter a length for the array: ");
            Scanner scan = new Scanner(System.in);
            int x = scan.nextInt();
    
            // assing new value to field
            randomNumbers = new int[x];
    
            for (int index = 0; index < randomNumbers.length; index++)
            {
                randomNumbers[index] = (int) (Math.random() * 100);
            }
    
            for (int index = 0; index < randomNumbers.length; index++)
            {
                System.out.println(randomNumbers[index]);
            }
    
        }
    
        void sortAscending()
        {
            Arrays.sort(randomNumbers);
    
            for (int i = 1; i < randomNumbers.length; i++) {
                System.out.println("Number: " + randomNumbers[i]);
            }
        }  
    
    }
    

【讨论】:

  • 很好的答案,谢谢!!
猜你喜欢
  • 1970-01-01
  • 2022-01-24
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
相关资源
最近更新 更多