Difference between ArrayList iList = new ArrayList(); and ArrayList iList = new ArrayList(); [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
ArrayList Generic without Type
5 answers
What is the difference between this two line:
ArrayList<Integer> iList = new ArrayList<Integer>();
ArrayList iList = new ArrayList<Integer>();
Step 1: raise a compile time error
public static void main(String args){
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // Compiler error, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}
Step 2: works fine
public static void main(String args){
ArrayList iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // works fine, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}
Output:
Value of 2nd element: Test_Element
I am expecting an error like
"add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.String)", but in step2 two it works fine.
Could anyone please explain me, why I am able to insert a String in this list.
java generics arraylist
marked as duplicate by Denys Séguret
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 7:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
ArrayList Generic without Type
5 answers
What is the difference between this two line:
ArrayList<Integer> iList = new ArrayList<Integer>();
ArrayList iList = new ArrayList<Integer>();
Step 1: raise a compile time error
public static void main(String args){
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // Compiler error, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}
Step 2: works fine
public static void main(String args){
ArrayList iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // works fine, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}
Output:
Value of 2nd element: Test_Element
I am expecting an error like
"add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.String)", but in step2 two it works fine.
Could anyone please explain me, why I am able to insert a String in this list.
java generics arraylist
marked as duplicate by Denys Séguret
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 7:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
check out stackoverflow.com/questions/25689673/…
– johnheroy
Jan 3 at 7:14
2
That is because of raw types. Don't use them.
– MC Emperor
Jan 3 at 7:17
You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList
– Vishwajit R. Shinde
Jan 3 at 7:29
add a comment |
This question already has an answer here:
ArrayList Generic without Type
5 answers
What is the difference between this two line:
ArrayList<Integer> iList = new ArrayList<Integer>();
ArrayList iList = new ArrayList<Integer>();
Step 1: raise a compile time error
public static void main(String args){
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // Compiler error, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}
Step 2: works fine
public static void main(String args){
ArrayList iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // works fine, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}
Output:
Value of 2nd element: Test_Element
I am expecting an error like
"add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.String)", but in step2 two it works fine.
Could anyone please explain me, why I am able to insert a String in this list.
java generics arraylist
This question already has an answer here:
ArrayList Generic without Type
5 answers
What is the difference between this two line:
ArrayList<Integer> iList = new ArrayList<Integer>();
ArrayList iList = new ArrayList<Integer>();
Step 1: raise a compile time error
public static void main(String args){
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // Compiler error, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}
Step 2: works fine
public static void main(String args){
ArrayList iList = new ArrayList<Integer>();
iList.add(10);
iList.add("Test_Element"); // works fine, while trying to insert a String in an Integer ArrayList
System.out.print("Value of 2nd element: " + iList.get(1));
}
Output:
Value of 2nd element: Test_Element
I am expecting an error like
"add(java.lang.Integer) in ArrayList cannot be applied to (java.lang.String)", but in step2 two it works fine.
Could anyone please explain me, why I am able to insert a String in this list.
This question already has an answer here:
ArrayList Generic without Type
5 answers
java generics arraylist
java generics arraylist
edited Jan 3 at 7:25
SKG
asked Jan 3 at 7:10
SKGSKG
192
192
marked as duplicate by Denys Séguret
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 7:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Denys Séguret
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 7:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
check out stackoverflow.com/questions/25689673/…
– johnheroy
Jan 3 at 7:14
2
That is because of raw types. Don't use them.
– MC Emperor
Jan 3 at 7:17
You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList
– Vishwajit R. Shinde
Jan 3 at 7:29
add a comment |
1
check out stackoverflow.com/questions/25689673/…
– johnheroy
Jan 3 at 7:14
2
That is because of raw types. Don't use them.
– MC Emperor
Jan 3 at 7:17
You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList
– Vishwajit R. Shinde
Jan 3 at 7:29
1
1
check out stackoverflow.com/questions/25689673/…
– johnheroy
Jan 3 at 7:14
check out stackoverflow.com/questions/25689673/…
– johnheroy
Jan 3 at 7:14
2
2
That is because of raw types. Don't use them.
– MC Emperor
Jan 3 at 7:17
That is because of raw types. Don't use them.
– MC Emperor
Jan 3 at 7:17
You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList
– Vishwajit R. Shinde
Jan 3 at 7:29
You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList
– Vishwajit R. Shinde
Jan 3 at 7:29
add a comment |
1 Answer
1
active
oldest
votes
There's no compile-time check on generic type of ArrayList
when you write ArrayList list = new ArrayList<Integer>()
. The generic type on the right side of the assignment expression is basically ignored. ArrayList
doesn't really have an inner type, just that the T
in ArrayList<T>
helps you make sure you only perform operations with instances of T
on your list.
It would be useful to understand why you want to have integers and strings mixed in the same ArrayList
--there might be a design smell here :)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There's no compile-time check on generic type of ArrayList
when you write ArrayList list = new ArrayList<Integer>()
. The generic type on the right side of the assignment expression is basically ignored. ArrayList
doesn't really have an inner type, just that the T
in ArrayList<T>
helps you make sure you only perform operations with instances of T
on your list.
It would be useful to understand why you want to have integers and strings mixed in the same ArrayList
--there might be a design smell here :)
add a comment |
There's no compile-time check on generic type of ArrayList
when you write ArrayList list = new ArrayList<Integer>()
. The generic type on the right side of the assignment expression is basically ignored. ArrayList
doesn't really have an inner type, just that the T
in ArrayList<T>
helps you make sure you only perform operations with instances of T
on your list.
It would be useful to understand why you want to have integers and strings mixed in the same ArrayList
--there might be a design smell here :)
add a comment |
There's no compile-time check on generic type of ArrayList
when you write ArrayList list = new ArrayList<Integer>()
. The generic type on the right side of the assignment expression is basically ignored. ArrayList
doesn't really have an inner type, just that the T
in ArrayList<T>
helps you make sure you only perform operations with instances of T
on your list.
It would be useful to understand why you want to have integers and strings mixed in the same ArrayList
--there might be a design smell here :)
There's no compile-time check on generic type of ArrayList
when you write ArrayList list = new ArrayList<Integer>()
. The generic type on the right side of the assignment expression is basically ignored. ArrayList
doesn't really have an inner type, just that the T
in ArrayList<T>
helps you make sure you only perform operations with instances of T
on your list.
It would be useful to understand why you want to have integers and strings mixed in the same ArrayList
--there might be a design smell here :)
answered Jan 3 at 7:16
johnheroyjohnheroy
32616
32616
add a comment |
add a comment |
1
check out stackoverflow.com/questions/25689673/…
– johnheroy
Jan 3 at 7:14
2
That is because of raw types. Don't use them.
– MC Emperor
Jan 3 at 7:17
You will not get any error, because your second type is Generic. You can add any type of object inside your second ArrayList
– Vishwajit R. Shinde
Jan 3 at 7:29