ArrayList的Java错误
我一直在浏览互联网,但找不到解决方案 这是我的密码:ArrayList的Java错误,java,arraylist,Java,Arraylist,我一直在浏览互联网,但找不到解决方案 这是我的密码: 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" ArrayLis
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
}
import java.util.ArrayList;
公开课回忆录{
int taille;//内存大小(位)
boolean flag=true;//如果内存已完全用完,则在“0”处。否则在“1”处
ArrayList pid=new ArrayList();//用于存储pid
ArrayList size=new ArrayList();//用于存储为每个pid分配的内存量
公开作废备忘录()
{
}
公共无效备忘录(int qTaille)
{
taille=qTaille;
}
public boolean allouer(int-qSize,int-qPid,boolean-force)//进程ID(pid)将分配的内存绑定到他的进程
{
int left=verifMemoire(qSize);//获取剩余内存
if(left
我仍然有Eclipse告诉我需要指定和对象到我的.add ArrayList(size.add(qSize);)中。这真的必要吗
这不仅是一个符合性错误。这是列表列表列表声明的方式,指定将存储在ArrayList中的元素类型。在这种情况下,我认为您是在
大小
列表中存储整数值,所以只需在尖括号内定义整数
类型,如下所示:
ArrayList<Integer> size = new ArrayList();
您需要指定arraylist将包含的对象类型
List myArrayList=new ArrayList()
或者在旧版本的java上,您需要指定两次
List myArrayList=new ArrayList()
请注意,此类型必须是类类型,而不是基元类型。不能使用int、char、double等(但可以使用Integer、Character、double)。此外,正如此答案中所添加的,在声明这些类型的变量时,最佳做法是使用列表接口类型而不是ArrayList类型
这就像您为数组和其他变量指定类型一样,但语法略有不同。如果您感兴趣,可以查找泛型,甚至只需查看ArrayList源代码即可I#m不复制错误,直到您告诉我们什么是verifMemoire(qSize)…verifMemoire(qSize)在哪里已定义??实际的错误消息是什么?我建议使用泛型来简化代码(和错误),但不确定您是否使用
ArrayList
,而不指定类型,并尝试添加int
。我认为它不起作用这需要两个“cast”要获取对象
您使用的是哪个版本的Java,您是否收到了错误或警告。看起来您应该使用泛型。可能是
ArrayList<Integer> size = new ArrayList<Integer>();
List<Integer> size = new ArrayList();