• Post Reply Bookmark Topic Watch Topic
  • New Topic

[boolean][int] array  RSS feed

 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have a 2 dim array with different types ie

2mimArray = new[boolean][int]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nikos Katsikanis:
Is it possible to have a 2 dim array with different types ie

2mimArray = new[boolean][int]


The index type is always int, but you could do the following:

X[][] array = new X[2][someLength];

...

array[flag? 1 : 0][col] = ...
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is X, and where do you define the parameters in the []?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was making up some code to demonstrate my solution. X was the array's sample component type. You have your own component type and your own index variables.

My point was that instead of trying to write things like array[false][y] or array[true][y], use 0 for false and 1 for true, for example.
[ June 05, 2006: Message edited by: Jeff Albertson ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not sure if you're trying to define an array that contains both booleans and ints, or if you're trying to use two different types to define how big your array should be.

can you clarify?

if it's the former, then no. an array can only hold one type of thing. now, that thing can be a class that can act like a boolean or an int, depending on how you construct it. but the array itself can only be of a single type.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to have each single dimension of the array to have only one type. But I one dimension of the would have a different typw of data.

ie

[int[] , boolean[]] each int value would have have an array of booleans.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is not what your definition says.
It says that you have an array of 2 elements, one of which is an array of ints and the other an array of booleans.

You can achieve that through the following code, but it's not something that is advisable.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, thats not it, my mistake, I want something like this

int[]Boolean[] mydata;
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nikos Katsikanis:
No, thats not it, my mistake, I want something like this

int[]Boolean[] mydata;


Since that's not Java, it's hard to understand what you want or what you're trying to achieve. perhaps if you wrote some pseudocode showing how you would use such a hypothetical array it would make it clearer. And what was wrong with my suggestions in previous posts? They certainly were legal Java...
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I read I believe you want this:



The "10" lengths would need to be replaced with the lengths you need, but
with this construct arr[i][0] can Store an Integer and arr[i][1] would be
an array of boolean values. Which is the meaning I interpreted from your
question.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx martin thats what I needed. Jeff, your code is fine, I am however not overly familiar with all the java syntax.

thanks again
 
stephen shields
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cannot declare the size of an array with a non integral type
other than that i dont too much see wat you are trying to do.

are you trying to have a 2d array as such

1 3 7 9 10
false true true false true
?
 
stephen shields
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cannot declare the size of an array with a non integral type
other than that i dont too much see wat you are trying to do.

are you trying to have a 2d array as such

1 3 7 9 10
false true true false true
?
 
stephen shields
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another thing
i think that you are misunderstanding the whole concept of arrays

an array is a series of common data "common for lack of use for the official term"

you can have an array with different data types, "one dimension or Kd where is is any integer"

you would have to create an array of integers and then another array of booleans.

hope this is helpful and would recommend you go back to the basics of data types
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!