4

new to Java. Trying to understand the point of declaring my ArrayList as an <Integer>. I still have to cast my .get() result as an int in my methods for it to work, else it still returns an Object. eg: (int) deliv.get(j) int the Sort method's for loop. Is there a way to avoid this or is my code the correct approach?

Problem: Assume the array can change size, hence not just using primitive array. All numbers should be pairs, looking for the unique one missing it's pair value. I sort the array, then cycle through the pairs to look for a mismatch. Thanks.

import java.util.*;
class StolenDrone{
public static void main(String[] args){
    ArrayList<Integer> deliv_id = new ArrayList<>();
    deliv_id.add(99);
    deliv_id.add(13);
    deliv_id.add(4);
    deliv_id.add(5);
    deliv_id.add(8);
    deliv_id.add(99);
    deliv_id.add(8);
    deliv_id.add(5);
    deliv_id.add(4);

    System.out.println("Array is: " + deliv_id);
    sort(deliv_id);
    System.out.println("Array is: " + deliv_id);
    int unique = findUnique(deliv_id);
    System.out.println("Unique ID is: " + unique);   
}

//Sort ArrayList into increasing order
static void sort(ArrayList deliv){
    int temp;
    for(int i = 0; i<deliv.size();i++){
        for (int j=0; j<deliv.size()-1;j++){
            if((int) deliv.get(j)> (int) deliv.get(j+1)){
                temp = (int) deliv.get(j+1);
                deliv.set(j+1, deliv.get(j));
                deliv.set(j, temp);
            }
        }
    }
}

//check pairs in ArrayList to find unique entry
static int findUnique(ArrayList deliv){
    for(int i = 0; i<deliv.size()-1;i+=2){
        if(deliv.get(i) == null){
            return -1; //no unique
        }
        if((int) deliv.get(i) != (int) deliv.get(i+1)){
            return (int) deliv.get(i);
        }
    }
    return -1;
}

}
2
  • 1
    You are mixing up reference (aka object) types like Integer with primitive types like int. Using ArrayList<Integer> makes get() to return Integer; and as Integer and int is not the same, you need to "cast". Commented Apr 22, 2015 at 10:35
  • 2
    should be ArrayList<Integer> deliv_id = new ArrayList<Integer>(); then no need to cast. Use Collections.sort(deliv_id) to sort your array Commented Apr 22, 2015 at 10:38

3 Answers 3

4

When you type parameterize ArrayList<Integer> the compiler knows that everything inside the ArrayList is of type Integer and will only allow you to add Integers to the list, and thus get() returns Integer. Without parameterizing the compiler will allow you to add any Object to the ArrayList, and thus calling get() will return an Object and requiring the cast to int.

To remove the need for casts you need to change parameters with type ArrayList to ArrayList<Integer> in the function declaration.

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

Comments

2

static void sort(ArrayList deliv)

Your method signature requests an untyped ArrayList. The compiler cannot know what will be inside the ArrayList so it requires you to cast the result.

Change it to this:

static void sort(ArrayList<Integer> deliv)

Now the compiler knows it is an ArrayList of Integers. So you wont need to add the cast to get()

Comments

2

In Java Integet is wrapper-class of int. You cannot set int as a type of ArrayList to work, but you can put there int type and it will be automatticly casted to Integer. To meke work it good you should do like this :

static void sort(ArrayList deliv){
    int temp;
    for(int i = 0; i<deliv.size();i++){
        for (int j=0; j<deliv.size()-1;j++){
            if(deliv.get(j)> deliv.get(j+1)){ // You should not cast, Integer is Comparable
                temp = deliv.get(j+1).intValue();//Changes here
                deliv.set(j+1, deliv.get(j).intValue());//And here
                deliv.set(j, temp);
            }
        }
    }
}

Good luck

1 Comment

This was my original code before I started casting (int), but returns to the error of comparing two objects.As others have pointed out, including the <Integer> label on the ArrayList argument being brought into the method makes this code valid. 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.