PDA

View Full Version : est.java uses unchecked or unsafe operations


TooCrooked
04-08-2010, 08:01 AM
help!

im trying to create a multidimensional arraylist to hold some values, but im getting warnings about "unchecked or unsafe operations".

here is the complete code:

import java.util.ArrayList;

public class test
{
public static void main(String args[])
{
ArrayList<Object> students = new ArrayList<Object>();

Boolean done = false;

do
{
// doesn't seem to matter if i generic ArrayList to an object
students.add(new ArrayList());
System.out.printf("Enter the name of student #%d: ", students.size());

((ArrayList<Object>)students.get(students.size() - 1)).add("test");

done = true;
}
while(!done);
}
}

when i compile it using the "-Xlint:unchecked" parameter, i'm told that:

test.java:17: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.ArrayList<java.lang.Object>
((ArrayList<Object>)students.get(students.size() - 1)).add("test");
^
1 warning

can anyone explain what i need to generic to get these warnings to go away??

Fou-Lu
04-08-2010, 01:34 PM
Its a warning since there is no guarentee that Object can become an ArrayList<Object>. Try specifying the actual generic with ArrayList<ArrayList<String>>, or Student if thats your class (this example shows a string). The other option is to try/catch the cast. BTW the use of Object completely defeats the purpose of generics.

TooCrooked
04-09-2010, 03:05 AM
Its a warning since there is no guarentee that Object can become an ArrayList<Object>.

which object? can you repost my code with some iinline comments for clarity?

Try specifying the actual generic with ArrayList<ArrayList<String>>

at what point?? the student variable is not intended to be a string in my program.

or Student if thats your class (this example shows a string).

student isn't a class. it's cast as an arraylist. can you post a code sample (of my code) with an inline comment that points out where "student" is a string?

can you post code that would fix the issue of the warnings by including the proper generics? im not interested in supressing the warnings, i actually want to address them. the program i've been writing works and that's all well and good, but i'd like to increase my understanding of why these warnings are occuring.

thanks for your help..

Fou-Lu
04-09-2010, 03:36 AM
No, I'm using my ps3 browser wich is tricky enough. Repace the students datatype with ArrayList<ArrayList<String>>. students.add will need an ArrayList<String>. Once changed, the get will return an ArrayList<String>, so no cast will be necessary.

TooCrooked
04-09-2010, 08:25 PM
ArrayList<ArrayList<String>>

can you explain this type of generic? you're explicitly defining that the arraylist will contain an arraylist of strings? is that right? is this a nesting of generics by any stretch?

Fou-Lu
04-10-2010, 06:18 AM
That is correct. ArrayList is just a datatype that allows generics. ArrayList<ArrayList> is simply an ArrayList where each item is an ArrayList. Since the ArrayList<ArrayList> specifies no generic, each inner ArrayList contains <? extends Object> (or just Object since any non-primitive implicitly extends Object already). Adding the String generic to become ArrayList<ArrayList<String>> now specifies that each inner item MUST be a String. So, when adding to the students, you must use students.add(new ArrayList<String>), but retrieving is much simpler with students.get(x) since it now returns an ArrayList<String> you will not need to cast before calling add on the results.

TooCrooked
04-10-2010, 06:35 AM
import java.util.ArrayList;

public class test
{
public static void main(String args[])
{
ArrayList<ArrayList<String>> students = new ArrayList<String>();

Boolean done = false;

do
{
// doesn't seem to matter if i generic ArrayList to an object
students.add(new ArrayList<String>());
System.out.printf("Enter the name of student #%d: ", students.size());

((ArrayList<String>)students.get(students.size() - 1)).add("test");

done = true;
}
while(!done);
}
}

test.java:7: incompatible types
found : java.util.ArrayList<java.lang.String>
required: java.util.ArrayList<java.util.ArrayList<java.lang.String>>
ArrayList<ArrayList<String>> students = new ArrayList<String>();

^
Note: test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

???

TooCrooked
04-10-2010, 08:04 AM
i talked to a guy at liveperson.net and was able to fix this:

import java.util.ArrayList;

public class test
{
public static void main(String args[])
{
ArrayList<ArrayList<Object>> students = new ArrayList<ArrayList<Object>>();

Boolean done = false;

do
{
students.add(new ArrayList<Object>());
System.out.printf("Enter the name of student #%d: ", students.size());

students.get(students.size() - 1).add("test");

done = true;
}
while(!done);
}
}

Fou-Lu
04-10-2010, 06:29 PM
Why are you using Object again? That will allow you to add new Integer(6) as a name. If each of your objects should be a string, generic it as such.

ArrayList<ArrayList<String>> students = new ArrayList<ArrayList<String>>();

TooCrooked
04-10-2010, 10:20 PM
whoops

TooCrooked
04-10-2010, 10:23 PM
it was you who suggested that each object should be a string. this was never my intention and i never stated this.i even asked you to back up why were assuming "student" was a string arraylist, and instead you forced your hand about changing things to a string.

i went with your <string> suggestion thinking it was the only way it would work because you decided not to even address my question about why you assumed i would only be adding strings to my arraylist. fortunately, once i spoke with someone who was able to actually compile/troubleshoot my work instead of guessing at it, i got a 100% working solution.

Fou-Lu
04-11-2010, 03:15 AM
Oh, pardon me. This: ((ArrayList<Object>)students.get(students.size() - 1)).add("test"); states you intend to use an ArrayList<ArrayList<String>>. If the data that "test" represents can be a: Double, JTree, Vector, Exception, ActionListener or anything non-primitive, then yes Object is what you want. On the other hand, if your not controlling this data upon insertion, yet want to allow only specific types use a generic representing the datatype in use; 'test' is a String, so assumably a string is your datatype.