Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Generics and sort: "unchecked method invokation" compiler warning

  1. #1
    larole is offline Member
    Join Date
    Aug 2012
    Location
    Linkping, Sweden
    Posts
    2
    Rep Power
    0

    Smile Generics and sort: "unchecked method invokation" compiler warning

    I get the following warning when compiling my code using NetBeans IDE option Clean and Build:
    warning: [unchecked] unchecked method invokation:
    <T>sort(java.util.List<T>) in java.util.Collections is applied to
    (java.util.ArrayList<ecf3.LinearEquation>)

    My code belongs to the package ecf3.

    Some code lines from the class LinearEquation :
    Java Code:
    public class LinearEquation implements Comparable {
    ...
    public int compareTo(Object equ) {
        LinearEquation equ2 = (LinearEquation )equ;
        if this.getMinFlowIndex() < equ2.getMinFlowIndex() {
            return -1;
        } else if this.getMinFlowIndex() == equ2.getMinFlowIndex() {
            return 0;
        } else {
            return 1;
        }
    } // compareTo
    Some code lines from the class Calculator :

    Java Code:
    public class Calculator extends Observable {
    ...
    private ArrayList<LinearEquation> vrlineq = new ArrayList<LinearEquation>();
    ...
    // Sort equations according to min index of flow
    Collections.sort(vrlineq);
    ...
    }
    How do I avoid the compiler warning by adding generics?
    The program works fine during runtime.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Generics and sort: "unchecked method invokation" compiler warning

    Try changing it to

    Java Code:
    public class LinearEquation implements Comparable<LinearEquation> { ... }
    and

    Java Code:
    public int compareTo(LinearEquation other) {
        return this.getMinFlowIndex() - other.getMinFlowIndex();
    }
    Get in the habit of using standard Java naming conventions!

  3. #3
    larole is offline Member
    Join Date
    Aug 2012
    Location
    Linkping, Sweden
    Posts
    2
    Rep Power
    0

    Default Re: Generics and sort: "unchecked method invokation" compiler warning

    Thank you, it works!

    Regarding naming convention, I have used Hungarian Notation with <prefix><type><Qualifier>.

    <type> is "vr" for vector, "i" for index, "sz" for (zero terminated) string etc.
    <type> is a problem oriented type, e.g. area, name, town, flow, tree, leaf etc.
    <Qualifier> tells what kind of object is meant, e.g. areaWall, areaRoof or flowWater flowGasoline or leafApple, leafMaple etc.

    I have seen different conventions but did not know about the Java Style rules. Thanks!

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Generics and sort: "unchecked method invokation" compiler warning

    Oh, yeah. That's just my signature, it wasn't directed at you. But it's important to know. It's so ingrained for most Java programmers that when people don't follow it, their code becomes hard to read.
    awinston likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. ArrayList - "Unchecked or unsafe operations" at compile
    By beachwood23 in forum New To Java
    Replies: 1
    Last Post: 03-23-2011, 05:01 AM
  2. Replies: 3
    Last Post: 02-16-2011, 06:39 PM
  3. Replies: 3
    Last Post: 05-25-2009, 08:50 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. How to solve "No compiler error"?
    By iceman in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 03:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •