如何在方法中使用主数组?

时间:2022-01-24 21:21:18

I want to take user input for merge sorting so i'm using the array ar[] in the method but it gives error "cannot find symbol " for ar[]..

我想把用户输入用于合并排序,所以我在方法中使用数组ar []但是它给ar []的错误“找不到符号”。

import java.util.*;
import java.io.*;
class Test
{
int Merge()
    {
    int q,p,r,i,l,m,j,t,k,w,x,s,u;
    w=q-p+1;
    x=r-q;
    int[] L=new int [w+1];
    int b=1;
    for(s=1;s<=w+1;s++)
        {
        L[b]=s;
        b++;
        }
    int[] R=new int [x+1];
    int c=1;
    for(t=1;u<=x+1;u++)
        {
        R[c]=u;
        c++;
        }
    for(i=1;i<=w;i++)
        {
        L[i]=ar[p+i-1];
        }
    for(j=1;j<=x;j++)
        {
        R[j]=ar[q+j];
        }
    L[w+1]=1000;
    R[x+1]=1001;
    i=1;
    j=1;
    for(k=p;k<=r;k++)
        {
        if(L[i]<=R[j])
            {
            ar[k]=L[i];
            i=i+1;
            }
        else 
            {
            ar[k]=R[j];
            j=j+1;
            }
        }
    System.out.println("sorted array"+ar[k]);
    }

public static void main(String ar[])
    {
    int a0=Integer.parseInt (ar[0]);
    int a1=Integer.parseInt (ar[1]);
    int a2=Integer.parseInt (ar[2]);
    int a3=Integer.parseInt (ar[3]);
    int a4=Integer.parseInt (ar[4]);
    int a5=Integer.parseInt (ar[5]);
    int a6=Integer.parseInt (ar[6]);
    int a7=Integer.parseInt (ar[7]);
    int a8=Integer.parseInt (ar[8]);
    int a9=Integer.parseInt (ar[9]);
    int p=a0,r=a9,q;

    if(p<r)
       q=(p+r)/2;

    Test T=new Test();
    T.Merge();
    }
}

3 个解决方案

#1


1  

ar is visible only in the scope of main method, it's unknown in other methods. In order to see it in other methods, you need to have a class member that will hold its value.

ar只在main方法的范围内可见,在其他方法中是未知的。为了在其他方法中看到它,您需要有一个保持其值的类成员。

#2


1  

You have an ar local variable in the main method, but you don't have it in the Merge method. Local variables, and method parameter is just another kind of local variable, are... well, local to the method where they are declared. That means that such a variable is undefined in another method.

main方法中有一个ar局部变量,但在Merge方法中没有它。局部变量和方法参数只是另一种局部变量,它们是声明它们的方法的本地变量。这意味着在另一种方法中未定义这样的变量。

For example, you can have

例如,你可以拥有

 class Test {
   final int[] ar;
   Test(int[] ar) { this.ar = ar; }

   public static void main(String[] ar) {
      ....
      final Test t = new Test(ar);
      t.Merge();
   }
 }

#3


0  

ar is will be visible in main method only, you need to create a new array in main method. Copy values read into new array and pass this array as input to merge method.

ar只在main方法中可见,你需要在main方法中创建一个新数组。将读取的值复制到新数组中,并将此数组作为输入传递给merge方法。

#1


1  

ar is visible only in the scope of main method, it's unknown in other methods. In order to see it in other methods, you need to have a class member that will hold its value.

ar只在main方法的范围内可见,在其他方法中是未知的。为了在其他方法中看到它,您需要有一个保持其值的类成员。

#2


1  

You have an ar local variable in the main method, but you don't have it in the Merge method. Local variables, and method parameter is just another kind of local variable, are... well, local to the method where they are declared. That means that such a variable is undefined in another method.

main方法中有一个ar局部变量,但在Merge方法中没有它。局部变量和方法参数只是另一种局部变量,它们是声明它们的方法的本地变量。这意味着在另一种方法中未定义这样的变量。

For example, you can have

例如,你可以拥有

 class Test {
   final int[] ar;
   Test(int[] ar) { this.ar = ar; }

   public static void main(String[] ar) {
      ....
      final Test t = new Test(ar);
      t.Merge();
   }
 }

#3


0  

ar is will be visible in main method only, you need to create a new array in main method. Copy values read into new array and pass this array as input to merge method.

ar只在main方法中可见,你需要在main方法中创建一个新数组。将读取的值复制到新数组中,并将此数组作为输入传递给merge方法。