Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法无法访问排序的数组_Java_Arrays_Sorting - Fatal编程技术网

Java 方法无法访问排序的数组

Java 方法无法访问排序的数组,java,arrays,sorting,Java,Arrays,Sorting,下面的代码按词典顺序对单词词典进行排序。问题是我无法访问for循环之外的排序数组。我想这就是问题所在。我在另一个方法中声明了数组字,并将其返回给同一类中的私有变量。但在我对数组进行排序并尝试打印出来之后,它只打印出原始数组。我还想提到,这个排序方法确实有效,因为我在另一个类中测试过它,所有内容都在主方法中。所以我再次相信我的问题与返回新的排序数组有关。在数组排序之后,我还需要在方法中编写几行代码,当我尝试使用“返回字”返回数组时,我无法编写此代码 public void insertionSor

下面的代码按词典顺序对单词词典进行排序。问题是我无法访问for循环之外的排序数组。我想这就是问题所在。我在另一个方法中声明了数组字,并将其返回给同一类中的私有变量。但在我对数组进行排序并尝试打印出来之后,它只打印出原始数组。我还想提到,这个排序方法确实有效,因为我在另一个类中测试过它,所有内容都在主方法中。所以我再次相信我的问题与返回新的排序数组有关。在数组排序之后,我还需要在方法中编写几行代码,当我尝试使用“返回字”返回数组时,我无法编写此代码

public void insertionSort() {
int in, out;

for (out=1; out<nElems; out++){
    String temp = words[out];                               // remove marked item
    in = out;                                               // start shifting at out                

    while (in>0 && words[in-1].compareTo(temp)>0){          // until temp is lexicographically after after the word it is being compared to
        words[in] = words[in-1];                            // shifts word to next position in lexicographical order
        --in;                                                   
    }
    words[in] = temp;
}
for(int i=0; i <words.length; i++)
    System.out.println(words[i]);
public void insertionSort(){
int-in,out;
for(out=1;out0&&words[in-1]。compareTo(temp)>0{//,直到temp按字典顺序位于与其进行比较的单词之后
单词[in]=单词[in-1];//按字典顺序将单词移到下一个位置
--在;
}
单词[in]=temp;
}
对于(int i=0;i
我已经用另一种方法声明了数组字

听起来您已经声明了两个数组:一个在另一个方法内,另一个在正在排序的类级别上完全独立。请确保两个方法使用相同的数组

如果希望它们都使用在类级别声明的数组,请不要在另一个方法中声明单独的数组。例如,如果在另一个方法中有
String[]words=new String[10];
,请将其更改为
words=new String[10];
。如果确实需要
nElems
变量(即,如果排序方法不能简单地使用
words.length
),请确保该变量也没有被重新声明,并且被设置为正确的值

或者,与将要排序的数据放在特定数组中不同,如果排序方法接受任何要排序的数组作为参数,则排序方法通常更有用:

public static void insertionSort(String[] words, int nElems) {
    /* ... rest of method as before ... */
}

我也将该方法设置为静态方法,因为它不再依赖于任何实例字段。

我理解您的意思,您在类级别使用私有变量,该变量从方法接收单词[]数组项……在这种情况下:

  • 确保类级别的私有变量是一个名为words的数组
  • 使私有变量(现在命名为words)的类型与从另一个方法接收的words数组的类型相同
  • 由于您是从insertionSort()调用Word,需要访问私有变量值,因此无法从另一个方法获取Word[]数组值,因为该数组是该方法的局部数组,因此必须将其值返回到某个数组变量(必须命名Word的私有变量),所以您必须访问该变量

  • nElems
    是如何定义的?我认为排序没有问题。您调用它或在其他方法中使用它的方式可能有问题,我们看不到这一部分。呜呜!我忘记定义nElems了。谢谢。