Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts

    est.java uses unchecked or unsafe operations

    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:

    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:

    Code:
    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??

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    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?

    Quote Originally Posted by Fou-Lu View Post
    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.

    Quote Originally Posted by Fou-Lu View Post
    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..
    Last edited by TooCrooked; 04-09-2010 at 03:08 AM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    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?
    Last edited by TooCrooked; 04-09-2010 at 08:33 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    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);
    	}
    }
    Code:
    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

    ???

  • #8
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    i talked to a guy at liveperson.net and was able to fix this:

    Code:
    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);
    	}
    }
    Last edited by TooCrooked; 04-10-2010 at 08:10 AM.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    Code:
    ArrayList<ArrayList<String>> students = new ArrayList<ArrayList<String>>();
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #10
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    whoops
    Last edited by TooCrooked; 04-10-2010 at 10:24 PM.

  • #11
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.
    Last edited by TooCrooked; 04-10-2010 at 10:28 PM.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #13
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    public class Student extends HttpServlet
    {
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws
    ServletException,IOException
    {
    //for holding from validation related error messages
    ArrayList al = new ArrayList();
    res.setContentType("text/html");
    PrintWriter pw = res.getWriter();
    //retrieving html form data in a servlet
    String sno = req.getParameter("stno");
    String sname = req.getParameter("name");
    String marks = req.getParameter("marks");
    int stno = 0;
    float stmarks=0.0f;
    //validation on student number
    if((sno==null)||(sno.equals("")))
    {
    al.add("provide student no");
    }
    else
    {
    try
    {
    stno=Integer.parseInt(sno);
    }catch(NumberFormatException nfe)
    {
    al.add("provide only int data for student no");
    }
    }//else
    //validation on student name
    if((sname==null)||(sname.equals("")))
    {
    al.add("provide name to student");
    }
    //validation on student marks
    if((marks==null)||(marks.equals("")))
    {
    al.add("provide student marks");
    }
    else
    {
    try
    {
    stmarks = Float.parseFloat(marks);
    }catch(NumberFormatException nfe)
    {
    al.add("provide only float data for student marks");
    }
    }//else
    if(al.size()!=0)
    {
    pw.println("<h1>"+al+"</h1>");
    }
    else
    {
    //code to store data into student database
    try
    {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection con = DriverManager.getConnection("jdbcracle:thin:@localhost:1521:SCOTT","scott","tiger");
    PreparedStatement ps = con.prepareStatement("insert into student values(?,?,?)");
    ps.setInt(1,stno);
    ps.setString(2,sname);
    ps.setFloat(3,stmarks);
    int res1 = ps.executeUpdate();
    if(res1>0)
    {
    pw.println("<h1>record inserted</h1>");
    }
    else
    {
    pw.println("<h1>record not inserted</h1>");
    }
    }catch(Exception e)
    {
    res.sendError(503,"problen in database");
    }
    }//else
    }//doGet
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
    doGet(req,res);
    }
    }//Student
    The compile error is as given below, how to rectify the error and compile the Program.
    Note:Student.java uses unchecked or unsafe operations.
    Note:Recompile with -Xlint:unckecked for details.
    Note:Recompile with -Xlint:unchecked for details.[/COLOR]


  •  

    Posting Permissions

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