Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:29:34+00:00 2026-05-20T09:29:34+00:00

Why am I getting these 4 warnings from -Xlint and what should I do

  • 0

Why am I getting these 4 warnings from -Xlint and what should I do about them? I’m just starting in Java, so am likely missing something obvious.

import java.util.*;

class CompareGerbils implements Comparator {
    public int compare(Object o1, Object o2) {
       return ((Gerbil)o2).number() - ((Gerbil)o1).number();
    }
}

class Gerbil {
int gerbilNumber;

Gerbil(int gN) {
    gerbilNumber = gN;
    }

int number() {
    return gerbilNumber;
    }
}

public class lt {
    public static void main(String[] args) { 

    // I'd like to be able to add both ints and strings to list
    ArrayList list = new ArrayList();

    //unchecked call warning:
    list.add(1);  

    //unchecked call warning:
    list.add("b");  

    ArrayList<Gerbil> gerbillist = new ArrayList<Gerbil>();

    for(int i = 0; i < 5; i++) {
        gerbillist.add(new Gerbil(i));
    }

    //unchecked conversion warning
    //unchecked method invocation
    Collections.sort(gerbillist, new CompareGerbils());
  }
}

EDIT: replies so far have answered the Arraylist declaration. How about the sort warnings at the bottom of the code? thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:29:35+00:00Added an answer on May 20, 2026 at 9:29 am

    You’re getting this because you have not defined a data type for the ArrayList list. The only way to add both Strings and Integers in list without getting warnings is by defining it as ArrayList<Object> list – which is what happens here implicitly (line list.add(1); is implicitly converting 1 to new Integer(1) – this is called autoboxing). Also note that if you want to have both Strings and Integers in lists, the sorting method does not really make sense – how are you expecting things to get sorted, alphabetically or numerically?

    Edit: Also, it is not considered good practice to declare a concrete type (i.e. ArrayList<Object> list) unless you have very good reasons to do so. It is recommended that you initialise using an interface, i.e. List<Object> list.

    So, your code would have to be like this (note the part Comparator<Gerbil> which fixes the warning in Collections.sort):

    // I'd like to be able to add both ints and strings to list
    List<Object> list = new ArrayList<Object>();
    
    list.add(new Integer(1));  
    
    list.add(new String("b"));  
    
    List<Gerbil> gerbillist = new ArrayList<Gerbil>();
    
    for(int i = 0; i < 5; i++) {
        gerbillist.add(new Gerbil(i));
    }
    
    Collections.sort(gerbillist, new Comparator<Gerbil>() {
        public int compare(Gerbil o1, Gerbil o2) {
            int diff = o1.getNumber() - o2.getNumber();
            if (diff > 0)
               return 1;
            else if (diff <0)
               return -1;
            else
               return 0;
        }
    });
    

    With respect to the Gerbil class, I suggest you use the form getNumber as a method name rather than number – it’s a de facto standard for method names to retrieve the value of a member variable (and, respectively setNumber(int value) for setting it):

    class Gerbil {
    int gerbilNumber;
    
    Gerbil(int gN) {
        gerbilNumber = gN;
        }
    
    int getNumber() {
        return gerbilNumber;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a lot of these warnings from 3rd party code that I
Getting these errors in some java code http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html Exception in thread Thread-0 java.lang.RuntimeException: Cannot
I've been getting these errors intermittently from sending email from my django app. I
Upgraded an app from 2.3 to 3.0 and now getting these. What's the best
I'm getting these warnings in my application, and it takes a long time get
After upgrading to new Android tools, I am getting following warnings from Proguard: Warning:
I'm often getting warnings about passing the wrong pointer types to functions and I
I am getting these two errors when retrieving meta data from a remote webpage.
I am getting deprecation warnings from nosetest for 3rd party modules imported by my
I just upgraded my application from Rails 2.3 to 3 and I'm getting some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.