Passing int[][] as generic parameter
up vote
10
down vote
favorite
public static <T> void func1(T arr) {
...
}
public static <T> void func2(T arr) {
...
}
I'm trying to pass a 2-dimensional array, int arr
.
I cannot use func1(arr)
, but I can use func2(arr)
Can someone explain me how this works?
java generics methods parameters parameter-passing
add a comment |
up vote
10
down vote
favorite
public static <T> void func1(T arr) {
...
}
public static <T> void func2(T arr) {
...
}
I'm trying to pass a 2-dimensional array, int arr
.
I cannot use func1(arr)
, but I can use func2(arr)
Can someone explain me how this works?
java generics methods parameters parameter-passing
add a comment |
up vote
10
down vote
favorite
up vote
10
down vote
favorite
public static <T> void func1(T arr) {
...
}
public static <T> void func2(T arr) {
...
}
I'm trying to pass a 2-dimensional array, int arr
.
I cannot use func1(arr)
, but I can use func2(arr)
Can someone explain me how this works?
java generics methods parameters parameter-passing
public static <T> void func1(T arr) {
...
}
public static <T> void func2(T arr) {
...
}
I'm trying to pass a 2-dimensional array, int arr
.
I cannot use func1(arr)
, but I can use func2(arr)
Can someone explain me how this works?
java generics methods parameters parameter-passing
java generics methods parameters parameter-passing
edited yesterday
Muntasir
6021818
6021818
asked 2 days ago
Sumit Das
524615
524615
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
20
down vote
T
represents an array of some generic object. Any array type (including int
) is an object. Therefore, int
is a valid T
when T = int
.
However, because int
is not an object, int
is not a valid T
.
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
2 days ago
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
2 days ago
add a comment |
up vote
1
down vote
If you you use Integer
instead of int
, you should be able to:
- call
func1
withInteger arr
- call
func2
withInteger arr
orInteger arr
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
20
down vote
T
represents an array of some generic object. Any array type (including int
) is an object. Therefore, int
is a valid T
when T = int
.
However, because int
is not an object, int
is not a valid T
.
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
2 days ago
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
2 days ago
add a comment |
up vote
20
down vote
T
represents an array of some generic object. Any array type (including int
) is an object. Therefore, int
is a valid T
when T = int
.
However, because int
is not an object, int
is not a valid T
.
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
2 days ago
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
2 days ago
add a comment |
up vote
20
down vote
up vote
20
down vote
T
represents an array of some generic object. Any array type (including int
) is an object. Therefore, int
is a valid T
when T = int
.
However, because int
is not an object, int
is not a valid T
.
T
represents an array of some generic object. Any array type (including int
) is an object. Therefore, int
is a valid T
when T = int
.
However, because int
is not an object, int
is not a valid T
.
answered 2 days ago
Joe C
9,81352341
9,81352341
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
2 days ago
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
2 days ago
add a comment |
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
2 days ago
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
2 days ago
4
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
2 days ago
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
2 days ago
3
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
2 days ago
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
2 days ago
add a comment |
up vote
1
down vote
If you you use Integer
instead of int
, you should be able to:
- call
func1
withInteger arr
- call
func2
withInteger arr
orInteger arr
add a comment |
up vote
1
down vote
If you you use Integer
instead of int
, you should be able to:
- call
func1
withInteger arr
- call
func2
withInteger arr
orInteger arr
add a comment |
up vote
1
down vote
up vote
1
down vote
If you you use Integer
instead of int
, you should be able to:
- call
func1
withInteger arr
- call
func2
withInteger arr
orInteger arr
If you you use Integer
instead of int
, you should be able to:
- call
func1
withInteger arr
- call
func2
withInteger arr
orInteger arr
edited 2 days ago
answered 2 days ago
TeeKea
833617
833617
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355630%2fpassing-int-as-generic-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown