-
package test;
-
-
import java.util.ArrayList;
-
-
public class MyArrayList{
-
public static void main(String args[])
-
{
-
ArrayList al = new ArrayList();
-
al.add("Jerald");
-
al.add("PJerald");
-
al.add(1);
-
}
-
}
-
I compiled my code as follows. It scold some thing that i dont know..
[$ ~]$ javac MyArrayList.java -d .; java test.MyArrayList
Note: MyArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
8 4326 -
package test;
-
-
import java.util.ArrayList;
-
-
public class MyArrayList{
-
public static void main(String args[])
-
{
-
ArrayList al = new ArrayList();
-
al.add("Jerald");
-
al.add("PJerald");
-
al.add(1);
-
}
-
}
-
I compiled my code as follows. It scold some thing that i dont know..
[$ ~]$ javac MyArrayList.java -d .; java test.MyArrayList
Note: MyArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Did you recompile with -Xlint unchecked for the details?
@OP: when Sun decided to introduce 'generics' they also decided that some
objects should be part of it: e.g. their Collection framework. This simply implies
that you don't just have an Arraylist anymore, you have an ArrayList that contains
a certain type, even if the type is just an Object. If you don't specify a type for
the objects to be stored in the ArrayList, all the compiler can do is whine at you.
Just because of backwards compatibility it has to accept that you want to store
just an object (or a derivative thereof) in your ArrayList, but it whines at you.
That's what you saw and you have two options:
1) ignore it and go on programming 'old style' which is semantically valid;
2) agnowledge the fact that you're just storing T type objects in an ArrayList.
For 2) you have to specify the type T using that funny <...> notation and you
have to fill in the dots as in: -
ArrayList<String> myList= new ArrayList<String>();
-
It's all about static type checking where the compiler is boss ...
kind regards,
Jos
@OP: when Sun decided to introduce 'generics' they also decided that some
objects should be part of it: e.g. their Collection framework. This simply implies
that you don't just have an Arraylist anymore, you have an ArrayList that contains
a certain type, even if the type is just an Object. If you don't specify a type for
the objects to be stored in the ArrayList, all the compiler can do is whine at you.
Just because of backwards compatibility it has to accept that you want to store
just an object (or a derivative thereof) in your ArrayList, but it whines at you.
That's what you saw and you have two options:
1) ignore it and go on programming 'old style' which is semantically valid;
2) agnowledge the fact that you're just storing T type objects in an ArrayList.
For 2) you have to specify the type T using that funny <...> notation and you
have to fill in the dots as in: -
ArrayList<String> myList= new ArrayList<String>();
-
It's all about static type checking where the compiler is boss ...
kind regards,
Jos
Oh I feel sorry jos and r035198x, since i signed out and left yesterday, but you people took pain and answered. Dear r035198x, then i compiled my code with -Xlint. It showed me three warnings something like this,
[$ ~]$ javac -Xlint MyArrayList.java
MyArrayList.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add("Jerald");
^
MyArrayList.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add("PJerald");
^
MyArrayList.java:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add(1);
^
3 warnings
Here with this
1) what is actually unchecked ?
2) should i specify explicitly the type of object which i going to add in the ArrayList( Am i wright jos?) ?
Thanks,
P.Jerald
Oh I feel sorry jos and r035198x, since i signed out and left yesterday, but you people took pain and answered. Dear r035198x, then i compiled my code with -Xlint. It showed me three warnings something like this,
[$ ~]$ javac -Xlint MyArrayList.java
MyArrayList.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add("Jerald");
^
MyArrayList.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add("PJerald");
^
MyArrayList.java:11: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
al.add(1);
^
3 warnings
Here with this
1) what is actually unchecked ?
2) should i specify explicitly the type of object which i going to add in the ArrayList( Am i wright jos?) ?
Thanks,
P.Jerald
Now read Jos' response above.
Now read Jos' response above.
i think my question number two is the answer!. ie., before we use ArrayList, We must specify what type of object we are going to store. am i right ?
i think my question number two is the answer!. ie., before we use ArrayList, We must specify what type of object we are going to store. am i right ?
Yep .
Yep .
k
thanks, r035198x.
regards,
P.Jerald
Note that you have to rethink the statement at line 11 of your op: a1.add(1).
An int is definitely not a String and it won't be 'autoboxed' to a String either.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Markus Elfring |
last post by:
I want to allow to display the contents of the directory
"/home/ftp/incoming/news".
Is the setting "safe_mode_include_dir"
(http://de.php.net/manual/en/features.safe-mode.php) the right one to...
|
by: Eric |
last post by:
Hi,
I'm getting this error message :
createSession(java.lang.String, java.lang.String,
org.omg.CORBA.IntHolder, B.SessionHolder,org.omg.CORBA.StringHolder)
in B.BOperations cannot be applied...
|
by: Steven Buroff |
last post by:
I can't seem to get the @SuppressWarnings("unchecked") to work.
Here is my test program.
public class Tryit {
@SuppressWarnings({"unchecked"})
public <T> T doit(Class<T> clazz){
T val =...
|
by: Stephen Gennard |
last post by:
Hello,
I having a problem dynamically invoking a static method that takes a
reference to a SByte*. If I do it directly it works just fine.
Anyone any ideas why?
I have include a example...
|
by: tlemcenvisit |
last post by:
Hello
I'm looking for the VC++.NET equivalent of "unsafe" instruction.
unsafe is a VC#.NET instruction
Thanks
|
by: bill salkin |
last post by:
How can I implement set operations in VB.NET. For example,
{1,2,3,4} union {3,4,5,6} = {1,2,3,4,5,6}
{1,2,3,4} minus {0,3} = {1,2,4}
{1,2,3,4,5,6,7} intersect {0,2,7} = {2,7}
Bill
|
by: Anders Borum |
last post by:
Hello!
I was looking at marking objects with a changed state, once properties have
been changed. The reason behind this is, that I would like to enlist such
objects for processing in a...
|
by: shengmin.ruan |
last post by:
i need to use the native code(unmanaged code),and i have to use
void*,so i use the "unsafe" symbol then i meet the trouble
c++code:
SC_InitAsk sca;
memset(&sca, 0, sizeof(sca));...
|
by: mailforpr |
last post by:
Hi.
Let me introduce an iterator to you, the so-called "Abstract Iterator"
I developed the other day.
I actually have no idea if there's another "Abstract Iterator" out
there, as I have never...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |