Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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_Compiler Errors - Fatal编程技术网

Java:找不到符号(数组类)

Java:找不到符号(数组类),java,arrays,compiler-errors,Java,Arrays,Compiler Errors,当我试图用Java向数组中添加整数时,我遇到了一点问题。该任务来自CodeWars,它将获取一个整数(如12345),并将其作为一个整数的反向数组(如{5,4,3,2,1})返回 现在,我的代码在eclipse上编译和运行,即使数组没有得到应有的返回,但我认为code wars为此提供了一个编辑过的toString类 无论如何,这是我的代码: package CodeWars; import java.lang.reflect.Array; public class Digitize {

当我试图用Java向数组中添加整数时,我遇到了一点问题。该任务来自CodeWars,它将获取一个整数(如12345),并将其作为一个整数的反向数组(如{5,4,3,2,1})返回

现在,我的代码在eclipse上编译和运行,即使数组没有得到应有的返回,但我认为code wars为此提供了一个编辑过的toString类

无论如何,这是我的代码:

package CodeWars;

import java.lang.reflect.Array;

public class Digitize 
{

    public static void main(String[] args) 
    {
        System.out.println(digitize(472837428));
    }

    public static int[] digitize(long n) 
      {
        String conv = Long.toString(n); 
        int[] result = new int[conv.length()];
        for(int i = 0, index = conv.length(); i == conv.length(); i++, index--)
        {
            int temp = Character.getNumericValue(conv.charAt(i));
            Array.setInt(result, index, temp);
        }
        return result;
      }
}
由于某种原因,代码战争不希望我们有主要的方法,下面是我粘贴的内容:

import java.util.*;
public class Kata 

{
  public static int[] digitize(long n) 
  {
    String conv = Long.toString(n); 
        int[] result = new int[conv.length()];
        for(int i = 0, index = conv.length(); i == conv.length(); i++, index--)
        {
            int temp = Character.getNumericValue(conv.charAt(i));
            Array.setInt(result, index, temp);
        }
        return result;
  }
}
然后,该代码在此处的检查方法上运行:

import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;

public class DigitizeExampleTests {
  @Test
  public void tests() {
    assertArrayEquals(new int[] {1, 3, 2, 5, 3}, Kata.digitize(35231));
  }
}
因此,当我在CodeWars中运行代码时,我会遇到这样的问题:

/workspace/java/src/Kata.java:12: error: cannot find symbol
            Array.setInt(result, index, temp);
            ^
  symbol:   variable Array
  location: class Kata
1 error
我已经在google上搜索过了,但是所有关于找不到符号的其他问题都是拼写错误的类或方法调用。有什么想法吗?作为一名Java初学者,如果您看到任何其他改进代码的方法,请务必告诉我


谢谢

它不知道
Array
是什么,因为
import java.lang.reflect.Array
缺失

但实际上你根本不应该使用反射

反射通常用于需要能够检查或修改Java虚拟机中运行的应用程序的运行时行为的程序。这是一个相对高级的特性,应该只由对该语言的基本知识有很强掌握的开发人员使用

您所需要的只是:

result[index] = temp;

它不知道什么是
Array
,因为缺少
import java.lang.reflect.Array

但实际上你根本不应该使用反射

反射通常用于需要能够检查或修改Java虚拟机中运行的应用程序的运行时行为的程序。这是一个相对高级的特性,应该只由对该语言的基本知识有很强掌握的开发人员使用

您所需要的只是:

result[index] = temp;

你的
for
循环条件是错误的(条件是何时继续,而不是何时停止),你的
索引是错误的。在循环的第一次迭代中,您会得到一个
ArrayIndexOutOfBoundsException
,但由于条件错误,循环永远不会运行。另外,正如其他人所说,只需直接访问数组而不进行反射。您的
for
循环条件是错误的(条件是何时继续,而不是何时停止),并且
索引是错误的。在循环的第一次迭代中,您会得到一个
ArrayIndexOutOfBoundsException
,但由于条件错误,循环永远不会运行。另外,正如其他人所说,只需直接访问阵列而无需反射。