4

errの部分はコードで大文字になっていますが、これもforeachingに含まれています。抽象リストのため、初期化できません。宣言は静的フィールドにあります。リストは同じタイプです。

import java.util.*;

public class Test
{

        public static final List<String> highPrio = Arrays.asList("*","/");
        public static List<String> ops;

        public static void main(String[] args)
        {
                //ERROR HERE, why do it throw nullPointer?
                ops.addAll(highPrio);

                for(String s : ops)
                {
                        System.out.println(s);
                }
        }
}

初期化で新しいList()を使用しないのはなぜですか?

初期化されなかった理由は、を使用できないことでした= new List<String>()。私はそれを許可しないロジックを見ることができません。それは、データ構造などの本質的な要因と関係があるに違いありません。

Test.java:7: java.util.List is abstract; cannot be instantiated public static List<String> ops = new List<String>();

なぜリストはインターフェースなのですか?

スタックなどの多くのデータ構造がリストを実装していることを私は知っています。しかし、なぜリストがインターフェースであるのか、そしてなぜテーブルではないのか、私には理解できません。リストは、他の構造を実装できる基本的な構造だと思います。インターフェイスは、構造の要件を指定できるものです。原始性または広大さはインターフェースである理由ですか?

4

7 に答える 7

12

opsがnullであるため。Listがインターフェースであるという事実は、フィールドを初期化できないことを意味するわけではありません。

public static List<String> ops = new ArrayList<String>();

List同じコントラクトを提供しながら(パフォーマンス特性は異なりますが)実装する方法が複数あるため、はインターフェイスです。たとえば、ArrayListは配列に裏打ちLinkedListされていますが、はリンクリストです。

于 2010-05-08T05:13:38.160 に答える
4

opsリストをインスタンス化する必要があります。

public static List<String> ops = new ArrayList<String>();

または選択した別のリストタイプ。

于 2010-05-08T05:13:44.150 に答える
1

opsが初期化されることはありません。

ops = new ArrayList<String>();addAllコマンドを実行する前に実行する必要があります。それ以外の場合は、nullオブジェクトを呼び出しています。

'できない理由ops = new List<String>は、Listがインターフェースであり、初期化できないためです。ArrayListは抽象型ではなく、Listを拡張するため、このコンテキストでは適切です。

抽象型とインターフェースは実際のオブジェクトとして作成することはできず、特定のオブジェクトを参照するためにのみ使用できます。具象型は、使用しようとしている抽象型またはインターフェースを拡張する必要があります。

于 2010-05-08T05:15:13.250 に答える
1

Listopsを初期化していません。

例えば

public static List<String> ops = new ArrayList<String>();

あるいは、あなたはすることができます

import java.util.*;

public class Test
{

    public static List<String> ops;

    public static void main(String[] args)
    {
            ops = Arrays.asList(new String[] {"*", "/"});

            for(String s : ops)
            {
                    System.out.println(s);
            }
    }
}
于 2010-05-08T05:17:00.727 に答える
1

opsはまだ初期化されていません。

宣言を次のように変更します。

public static List<String> ops = new ArrayList<String>();
于 2010-05-08T05:17:17.590 に答える
0

I think your real question is:

Why can't I instantiate a List?
or Why am I getting: Test.java:7: java.util.List is abstract; cannot be instantiated public static List<String> ops = new List<String>();

In Java, List is an interface, which is, as you say: Interface is a thing where you can specify requirements for a structure - its like a job description that needs to be filled. But you can't clip the job description out of the paper and simply use it to do the job, i.e. the List job description is abstract; cannot be instantiated.

Instead, you need candidates to fill the position described by the job description for List. The candidates ("concrete implementations") that meet the requirements for the job are ArrayList, LinkedList, Vector, etc. Unless you initialize your List<String> ops var with a specific candidate to do the job, you've got no one (null) to actually do the work (there by raising a NullPointerException.

List<String> ops = new ArrayList<String>();
于 2010-05-08T10:20:35.223 に答える
0

あなたは何に追加していますか?次のnullとops同様に、変数はnullのままです。s

public static String s;
于 2010-05-08T05:37:31.200 に答える