-3

I've been browsing the internet and I couldn't manage to find a solution.

Here is my code :

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
}

I still have Eclipse telling me that I need to specify and object to my .add ArrayList ( size.add(qSize); ). Is it really necesseary?

It's not only a compliance error. It's the way a List ist declared

Amperclock
  • 379
  • 2
  • 16
  • I#m not abe to reproduce the error until you tell us what is ***verifMemoire(qSize)***....where is ***verifMemoire(qSize)*** defined?? – ΦXocę 웃 Пepeúpa ツ Apr 24 '17 at 09:01
  • What is the actual error message? I suggest using generics to simplify your code (and errors) – Peter Lawrey Apr 24 '17 at 09:03
  • Not sure but you use a `ArrayList` without specifying the type and try to add an `int`. I don't think it will work this need two "cast" to get an `Object` – AxelH Apr 24 '17 at 09:04
  • Which version of Java are you using, are you getting an error or warning. Looks like you should be using generics. – Saurabh Jhunjhunwala Apr 24 '17 at 09:05
  • 2
    Possible duplicate of [What is "compiler compliance level" in Eclipse?](http://stackoverflow.com/questions/22584427/what-is-compiler-compliance-level-in-eclipse) – Tom Apr 24 '17 at 09:07
  • Use a compliance level which supports autoboxing. – Tom Apr 24 '17 at 09:07
  • @AxelH No, this generally works, even with using a raw list. – Tom Apr 24 '17 at 09:11
  • @Tom, yeah wasn't sure, thanks. I am thinking about something else but I can't remember where this occurs ... well don't use raw type in general – AxelH Apr 24 '17 at 09:21
  • @AxelH What you've explained won't work if one uses Java prior 1.5 where Autoboxing where implemented. So the compiler won't box `int` to `Integer` and what won't work, neither for `ArrayList`, nor a raw `ArrayList`. Maybe you had that in mind. – Tom Apr 24 '17 at 09:23

2 Answers2

1

Specify the type of elements that will be stored in the ArrayList. In this case I think you are storing integer values in size list, so just define Integer type inside angle brackets like this:

ArrayList<Integer> size = new ArrayList();

If you are using pre Java 7 version:

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

By the way, I would suggest you to use List interface type on the left side of assignment and specify implementation of List (like ArrayList) only on the right side. This way you will decouple your code from specific implementation of the interface:

List<Integer> size = new ArrayList();
Vaidas
  • 1,494
  • 14
  • 21
1

You need to specify what type of object your arraylist will contain.

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

Or on older versions of java you'll need to specify twice

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

Note that this type must be a class type, and not a primitive type. You can't use int, char, double etc. (but you can use Integer, Character, Double). In addition, as gundev added to this answer, best practice is to use the List interface type rather than the ArrayList type when declaring these types of variables

This is just like how you specify types for arrays, and other variables, but with a slightly different syntax. If you're interested you can look up generics or even just look at the ArrayList source code

Community
  • 1
  • 1
Luke K
  • 865
  • 6
  • 13