Unsafe or unchecked operations for ArrayList

南笙酒味 提交于 2019-12-19 02:58:07

问题


I'm been assigned to make a program that gets 100 random integers between 0-25 and store them in an array. I then have to call upon 2 methods to split the evens and the odds (very typical). So I tried the ArrayList thing (I jut learnt it) and it seemed fine (I was following tutorial and things online) until I ran into this: Unit8.java uses unchecked or unsafe operations

My code is this:

    import java.util.*;
    import java.awt.*;

    public class Unit8
    {
public static void main (String [] args)
{
    //create an array for original 100 integers
    //create a 2D array for evens and odds
    //split them up using 2 methods

    int[] originalArray = new int[100];
    ArrayList even = new ArrayList(1);
    ArrayList odd = new ArrayList(1);

    for (int x = 0; x < originalArray.length; x++)
    {
        originalArray[x] = (int)(Math.random() * 25);
    }

    evensDivider(originalArray, even);
    oddsDivider(originalArray, odd);
}

public static void evensDivider (int[] e, ArrayList even)
{


    for (int y = 0; y < e.length; y++)
    {
        if (e[y]%2 == 0)
            even.add(e[y]);
    }

    System.out.println("The evens are: " + even);
}

public static void oddsDivider (int[] o, ArrayList odd)
{


    for (int z = 0; z < o.length; z++)
    {
        if (o[z]%2 == 1)
            odd.add(o[z]);
    }
}

}

With the errors occurring specifically at: even.add(e[y]); and odd.add(o[z]);

Please Help me out with this, I've tried my best to make it clear and easy to understand.


回答1:


This is because you are using ArrayList with raw type. And you are adding a specific type to it.

Raw type ArrayList would expect element of type Object. If you add any other type, then Compiler would not know exactly what type you are storing. So, it gives you unchecked or unsafe operations to warn you that you might be doing something wrong.

You should better create a Generic ArrayList:-

List<Integer> evenNumbers = new ArrayList<Integer>();

Also, change it in your method signature: -

public static void evensDivider (int[] e, List<Integer> evenNumbers)

PS: - You should always have a reference of interface type if you have one. I mean use List in place of ArrayList




回答2:


It's complaing about this

ArrayList even = new ArrayList(1);

you have the non-generic version of ArrayList there. Change it to

List<Integer> even = new ArrayList<Integer>(1);

That is generic and type-safe.

Also (as a side note) the value 1 in new ArrayList(1); specifies the initial capacity of the list. That feature is there for situations where you know you will be adding a huge number elements to the list, in which case you would provide some bigger number, which would help avoid the resizing overhead. Passing a small value (like 1) doesn't make sense.




回答3:


Instead of this:

ArrayList even = new ArrayList(1);

try this:

List<Integer> evens = new ArrayList<Integer>(50);

Here are the advantages:

  • This List can only hold numbers (Integers, actually, also known as ints), which I'm assuming is where the warning is coming from.
  • it has a plural name, which better reflects what it is, a list
  • It instantiates itself with a size of 50 (rather than 1), which is about how big you expect it to be.



回答4:


Replace this ArrayList even = new ArrayList(1); with ArrayList<Integer> even = new ArrayList<Integer>(1);

Or add @SuppressWarnings("rawtypes") on the top of the attribute/method/class.



来源:https://stackoverflow.com/questions/12947806/unsafe-or-unchecked-operations-for-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!