0

I am trying to add int values to an int[] as follows.

 private ArrayList<int []> read_studioConfig(byte[] buf, int boundary, int offset, int num){

    ArrayList<int []> configs_values = new ArrayList<int[]>();

    int readValues = 0;

    int idx = offset;
    for (int i = 0 ;i < num; i++){
         while(idx < boundary && buf[idx] != 0){
             readValues = i;
             idx ++;
         }
         idx ++;

        configs_values.add(readValues);
    }

    return configs_values;
};

But I could not able to do it. I am getting the following error.

error: no suitable method found for add(int) method Collection.add(int[]) is not applicable (argument mismatch; int cannot be converted to int[]) method List.add(int[]) is not applicable (argument mismatch; int cannot be converted to int[])**

I am doing a stupid mistake but can't put finger on it. What am I missing here ?

8
  • 1
    I think you want: ArrayList<Integer> Commented Oct 24, 2019 at 15:11
  • 1
    To wit: right now you're creating an array list of int arrays. Commented Oct 24, 2019 at 15:13
  • @ErnieThomason Changing that solves the problem. But I would like to add values arraylist of int arrays Commented Oct 24, 2019 at 15:15
  • Java collections stores objects, not primitive types. To have 2-D arrays, use ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>(); Commented Oct 24, 2019 at 15:16
  • 2
    If you want to add arrays, pass arrays, not ints. Commented Oct 24, 2019 at 15:17

1 Answer 1

1

Firstly you're creating a List of Arrays of integers, so that is the reason your code is failing.

I'm not sure why you're trying to do due to your code is a little bit dirty, but here you have my suggestion:

Use List<Integer> configsValue = new ArrayList() rather than ArrayList<int[]>, use interfaces rather than implementations. Also if you don't need to keep direct access even I'd suggest to you to use LinkedList instead ArrayList because the complexity of adding a new element is O(1) against O(n)

Then your code will work, otherwise if you want to return a List<int[]> be aware of you have to create the array before adding it to the collection

Sign up to request clarification or add additional context in comments.

1 Comment

that clears up a lot. I will look into that direction man. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.