Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.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/6/apache/9.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 将列表更改为ArrayList将删除错误;列表是抽象的,不能实例化?_Java_List_Arraylist_Abstract Data Type - Fatal编程技术网

Java 将列表更改为ArrayList将删除错误;列表是抽象的,不能实例化?

Java 将列表更改为ArrayList将删除错误;列表是抽象的,不能实例化?,java,list,arraylist,abstract-data-type,Java,List,Arraylist,Abstract Data Type,嗯,我是列表的新手我一直使用数组,现在我正在编写一个程序,在创建数组之前我无法知道数组的大小,所以我使用列表。问题是我有一个方法,它返回一个无重复数字的列表 这是我的方法: public static List<Integer> arrayOfNoneRepeatingDigits(int limit) { List<Integer> list = new ArrayList<Integer>(); for(int i = 0; i < limit;

嗯,我是
列表的新手
我一直使用
数组
,现在我正在编写一个程序,在创建数组之前我无法知道数组的大小,所以我使用
列表
。问题是我有一个方法,它返回一个无重复数字的列表

这是我的方法:

public static List<Integer> arrayOfNoneRepeatingDigits(int limit) {

List<Integer> list = new ArrayList<Integer>();

for(int i = 0; i < limit; i++){
        boolean ignore = false;
        for(int j = i; j > 0; j/=10){
            if(ignore == true) break;
            for(int k = j/10; k > 0; k/=10){
                if(j%10 == k%10){
                    ignore = true;
                    break;                        
                    }                    
                }               
            }
        if(ignore == false)list.add(i);                
        }    
return list;    
} 
公共静态列表数组fNoneRepeatingDigits(整数限制){
列表=新的ArrayList();
对于(int i=0;i0;j/=10){
如果(忽略==真)中断;
对于(int k=j/10;k>0;k/=10){
如果(j%10==k%10){
忽略=真;
打破
}                    
}               
}
if(ignore==false)list.add(i);
}    
退货清单;
} 
首先,我的方法数据类型是一个数组,但在我将其更改为列出这行代码后,出现了一个错误:

List<Integer> list = new List<Integer>();
List List=新列表();
我在网上搜索过,结果发现我应该用ArrayList替换列表。我这样做了,错误现在消失了,但我不知道为什么。

这个:

List<Integer> list = new List<Integer>();
List List=新列表();
没有意义,因为您试图直接实例化一个接口,这是不具体的,因此除非您使用实例化创建一个匿名的内部类,否则无法执行,这是您真正不想执行的。最好的解决方案是:坚持使用ArrayList来实现具体的实现(或者根据需要使用LinkedList)。e、 g

List List=new ArrayList();


出于好奇,当您已经有了工作代码时,是什么促使您进行此更改的?

接口无法实例化,因此没有新的
列表。接口的方法没有实现。这些方法的实现在子类中
ArrayList
List
的子类之一,因此您可以使用第一个代码段


如果您编写
新列表
,则没有意义。因为
List
的方法没有实现,所以当您调用
add
方法时,编译器如何知道方法中有什么实现?

List是一个接口。这意味着列表中并没有属性,方法被声明但并没有定义。因此,没有足够的信息来实例化它,因此发生了错误。当您尝试实例化抽象类的对象时,也会发生这种情况

您可以声明接口/抽象类的对象,但不能实例化它。所以你有两个选择

List<Integer> list = new ArrayList<Integer();
((ArrayList)list).method(); 
//cast it into ArrayList. 
//If you try to cast a wrong class, then the error will be printed out.

List List=new ArrayList是一个接口,而不是一个具体的类。除了您关于
List
接口的主要问题外,您能否澄清一下您试图用您的程序实现什么?或者给出输入和预期结果的示例。您的代码似乎可以通过几种方式进行改进。当然,如果你有兴趣学习和提高。@HaroutTatarian谢谢你的澄清,我不会破坏解决方案。但是我想知道它和你问题中的代码有什么关系。如果我没弄错的话,您的代码会尝试查找范围内没有重复数字的所有数字。原来的问题(至少是我想到的解决方案)似乎不需要这样的子程序。@HaroutTatarian哦,我明白了。但是仍然不清楚为什么需要这个子程序。您是否有可能在编号1、2、…、9(按此顺序)
之间的
行中遗漏了此顺序?不过,我希望我不要太打扰别人。我相信你最终会解决这个问题,甚至可能用一些非规范的方法。@Aivan谢谢你,我解决它后会告诉你的。为什么要强制转换:
((ArrayList)list.method()?既然您将只使用列表界面的方法,那么有什么必要这么做呢?有一些方法像toArray好的,我买它。1+来抵消一些被否决的选民(不确定为什么被否决)。@HovercraftFullOfEels我将解释为什么被否决。这个答案会让新手感到困惑。根据经验,如果可能,最好使用接口。因此,将列表实例化为
list list=new ArrayList()是非常好的然后将其作为接口使用。在极少数情况下,当确实需要使用具体的实现时,例如
ArrayList
,最好将其声明为
ArrayList
,而不是强制使用access。感谢您的回答!关于你的问题,我不明白你说的是什么变化。@HaroutTatarian:变化是你已经有了
new ArrayList()
,它工作了,但试图使用不起作用的
new List()
。为什么?不,我首先使用了“new List()”,然后在网上搜索后对其进行了更改。
List<Integer> list = new ArrayList<Integer();
((ArrayList)list).method(); 
//cast it into ArrayList. 
//If you try to cast a wrong class, then the error will be printed out.
ArrayList<Integer>list = new ArrayList<Integer>();