Passing int[][] as generic parameter
up vote
14
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
14
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
14
down vote
favorite
up vote
14
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 Nov 18 at 9:54
Muntasir
6041818
6041818
asked Nov 17 at 21:12
Sumit Das
544615
544615
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
23
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
Nov 17 at 22:28
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
Nov 18 at 6:25
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
23
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
Nov 17 at 22:28
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
Nov 18 at 6:25
add a comment |
up vote
23
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
Nov 17 at 22:28
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
Nov 18 at 6:25
add a comment |
up vote
23
down vote
up vote
23
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 Nov 17 at 21:14
Joe C
10k52341
10k52341
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
Nov 17 at 22:28
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
Nov 18 at 6:25
add a comment |
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
Nov 17 at 22:28
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
Nov 18 at 6:25
4
4
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
Nov 17 at 22:28
To expand on this, you could change it to Integer arr; and it should work.
– LadyCailin
Nov 17 at 22:28
3
3
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
Nov 18 at 6:25
@LadyCailin true, but in most cases when you need multi-dimensional arrays, it is really bad idea to use wrapper types
– user1643723
Nov 18 at 6:25
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 Nov 18 at 0:55
answered Nov 17 at 23:56
TeeKea
1,71111023
1,71111023
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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