2017-04-24 10 views
-3

私はインターネットを閲覧していましたが、解決策を見つけることができませんでした。 (size.add(QSIZE);)私はまだEclipseは私が指定して、私の.add ArrayListに反対する必要があることを私に言っていArrayListのJavaエラー

import java.util.ArrayList; 

public class Memoire{ 

int taille;        //Size of the memory in bits 
boolean flag = true;     //At "0" if the memory is fully utelized. Else at "1" 
ArrayList pid = new ArrayList();  //Use to store the pid 
ArrayList size = new ArrayList();  //Use to store the amount of memory allocated for each pid 

public void memoire() 
{ 

} 

public void memoire(int qTaille) 
{ 
    taille = qTaille; 
} 

public boolean allouer(int qSize, int qPid,boolean force)//The Process ID (pid) bind the allocated memory to his process 
{ 
    int left = verifMemoire(qSize);     //Get the remaining memory 

    if(left < qSize)        //If the remaning memory is larger than the request 
    { 
     pid.add(qPid);        //Store the pid 
     size.add(qSize);       //Store the size 
    } 
    else 
    { 
     if(force)         //If the user want to force the allocation 
     { 
      pid.add(qPid);       //Store the pid 
      size.add(left);       //Sotre the remaining memory size 
     } 
     else 
     { 
      return false;       //If the user don't want to force, return an error 
     } 
    } 
    return true;         //Return the success of the allocation 
} 

は、ここに私のコードです。本当に必要ですか?

これはコンプライアンスエラーだけではありません。リストが宣言される方法です

+0

*** verifMemoire(qSize)***を教えてくれるまで、私はエラーを再現しません。 *** verifMemoire(qSize)***が定義されていますか? –

+0

実際のエラーメッセージとは何ですか?あなたのコード(とエラー)を単純化するためにジェネリックを使うことをお勧めします。 –

+0

確かに型を指定せずに 'ArrayList'を使い、' int'を追加しようとします。私はそれが "オブジェクト"を取得するために2つの "キャスト"この必要性を動作するとは思わない – AxelH

答えて

1

ArrayListに格納される要素のタイプを指定します。この場合、私はあなたがsizeリストに整数値を格納していると思うには、これだけのようIntegerタイプ内部の角括弧を定義:することにより

ArrayList<Integer> size = new ArrayList<Integer>(); 

:あなたは、事前のJava 7のバージョンを使用している場合

ArrayList<Integer> size = new ArrayList(); 

ところで、割り当ての左側にListインターフェイスタイプを使用し、右側にリストの実装(ArrayListなど)を指定することをお勧めします。このようにして、コードをインタフェースの特定の実装から切り離します。

List<Integer> size = new ArrayList(); 
1

arraylistに含まれるオブジェクトのタイプを指定する必要があります。

List<Type> myArrayList = new ArrayList<>()

やJavaの古いバージョンにあなたは二回

List<Type> myArrayList = new ArrayList<Type>()

(注)この型がクラス型ではなく、プリミティブ型でなければならないことを指定する必要があります。 int、char、doubleなどは使用できません(ただし、Integer、Character、Doubleを使用できます)。また、この回答にgundevが追加されているため、これらの型の変数を宣言するときに、ArrayList型ではなくListインターフェイス型を使用することをお勧めします。

これは配列やその他の変数の型を指定するのと同じですわずかに異なる構文を使用します。興味があれば、ジェネリックを探したり、ArrayListのソースコードを見ることもできます