Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2010, 08:01 AM   PM User | #1
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
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??
TooCrooked is offline   Reply With Quote
Old 04-08-2010, 01:34 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,747
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 04-09-2010, 03:05 AM   PM User | #3
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
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..
TooCrooked is offline   Reply With Quote
Old 04-09-2010, 03:36 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,747
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 04-09-2010, 08:25 PM   PM User | #5
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
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..
TooCrooked is offline   Reply With Quote
Old 04-10-2010, 06:18 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,747
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 04-10-2010, 06:35 AM   PM User | #7
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
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

???
TooCrooked is offline   Reply With Quote
Old 04-10-2010, 08:04 AM   PM User | #8
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
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..
TooCrooked is offline   Reply With Quote
Old 04-10-2010, 06:29 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,747
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 04-10-2010, 10:20 PM   PM User | #10
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
whoops

Last edited by TooCrooked; 04-10-2010 at 10:24 PM..
TooCrooked is offline   Reply With Quote
Old 04-10-2010, 10:23 PM   PM User | #11
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
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..
TooCrooked is offline   Reply With Quote
Old 04-11-2010, 03:15 AM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,747
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 07-02-2012, 05:17 PM   PM User | #13
Pavan6
New to the CF scene

 
Join Date: Jul 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Pavan6 is an unknown quantity at this point
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]
Pavan6 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:57 PM.


Advertisement
Log in to turn off these ads.