Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Processing: How to store and group int array values

array

  • Please log in to reply
20 replies to this topic

#1 kalimann

kalimann

    CC Regular

  • Member
  • PipPipPip
  • 40 posts
  • Learning:Java, C#

Posted 07 October 2012 - 01:55 AM

Using the language Processing which is based off Java, I want to group and store values from an int array. So I can later call an int[][] and the program will tell me which group it is categorised under.

How to do that?
  • 0

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 07 October 2012 - 03:26 AM

Hello,
I am not sure i understood the question but i guess you are trying to find Java's native data types, since Arrays in all its forms (list, queues, dictionaries) are actually objects.

Here is the documentation about the native data types

i gave you that thinking you might not be looking on how to work with the Array() object, but with contiguous memory , even when you can stream or serialize an Array() or derivate
  • 0

#3 kalimann

kalimann

    CC Regular

  • Member
  • PipPipPip
  • 40 posts
  • Learning:Java, C#

Posted 07 October 2012 - 04:45 AM

I have an int array, lets call it Grid[][].

I have some loops, which uses its values for making a grid in a level in a game. How can make the program categorize the Grid[][] individual values (for example x1, y1 under category 1 and x2, y2 under category 2) according to which loop they belong under, so the program can check a value (for example Grid[x2][y2]) and check - ahh it belongs under category 2!
  • 0

#4 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 07 October 2012 - 02:32 PM

I'm confuse ..... Can you just make 2 list

List<int> xList = new List<int>()

add the numbers ... then apply the samething for y. Then create the grouping

or you can use HashTable where you can have <key, value>.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#5 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 07 October 2012 - 02:34 PM

make the program categorize the Grid[][] individual values

What do you mean by a category?

x1, y1 under category 1

What is the relationship of x1 and y1 with a 2 dim array?

Grid[x2][y2]) and check - ahh it belongs under category 2

Here x2 and y2 are used as indexes into the array. Can the values of x1 and y2 change so that they can index any of the elements in the array? Not sure what "under category 2" means if all of the elements can be indexed by x2 & y2.
  • 0

#6 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 07 October 2012 - 05:44 PM

If you create a 2 dimensional array, and, lets say you want to put an int at x1, y1, where x1 is the first dimension, and y1 is the second dimension (array[1][1]), then you will create a value at [1][1]. Think of it like this. You have each row, the first dimension, which is array[6]. It would look like this:

[0][1][2][3][4][5]

If you added a second dimension, array[6][6], then you get something like this:

[0-0][1-0][2-0][3-0][4-0][5-0]
[0-1][1-1][2-1][3-1][4-1][5-1]
[0-2][1-2][2-2][3-2][4-2][5-2]
[0-3][1-3][2-3][3-3][4-3][5-3]
[0-4][1-4][2-4][3-4][4-4][5-4]
[0-5][1-5][2-5][3-5][4-5][5-5]

You can think of the first dimension as x, and the second as y (array[x][y]). I didn't really understand the question, but if you wanted to store a value at (1,2) on the level, and it was using a 2 dimensional array, as long as the 2 coordinates are also values on the array, you could just use array[1][2] = value to store the value, and array[1][2] to get the value. Just think of it as array[x][y].

It is not recommended to make a single reference, but you could make a container with a 1 dimensional array, and store values depending on a certain difference the all share. For example, if you had 2 values on an x of 1, you could make a container with a 1 dimensional array, 2 indexes long, and put each value in an index. Since they all share the same, x, you would only need the y value to find them. Otherwise, it is not advised to store 2 values in one (you cannot store an x and a y as a single int, without mess ups here and there).

I recommend grouping them based on x, or y. If you used the container idea from above, you could put each value on your map into a container, and place it on the array. If each container also contained the coordinates of the value, you could iterate through the array of containers, and find the one that is equal in value, or has a similar x/y to the one you want. You could also just use the index of the container containing the value on the array of containers as an identifier.

Hope this helped.
  • 1
Speaks fluent Java

#7 kalimann

kalimann

    CC Regular

  • Member
  • PipPipPip
  • 40 posts
  • Learning:Java, C#

Posted 07 October 2012 - 11:48 PM

Thanks everyone, I see my question was a bit confusing, my pardons, I'll try to cut it out black and white.

I have a two dimensional array, lets call it grid[][]. Grid contains x and y for the 2d grid in the game. The grid has a width of 40 and a height of 30.

So lets say that I want to be able to check a cell in this grid, and see whether it belongs to the "Walls" group or the "Hollow" group. Some of the cells are randomized for game's sake, so I have to make the program able to put the value in a category as soon as one of the methods are used. I have made a Wall() and a Hollow() method for this, they change the color of the grid cell.. and that's about it. But I could easily make the method store the individual value someplace at the same time, if only I knew how.

Problem is I don't know how to make a variable store several values belonging to an array. Do I create another array to store each of them? So I have a grid[][], and wall[][] and hollow[][] which can then check. "is grid[1[[1] also known as wall[1][1]. No? Is it also known as hollow[1][1]? Yes? It is a hollow cell then."

I suppose I could make it that way, but what I am looking for is an easy method to do the before mentioned. Or rather, easier/convenient.
  • 0

#8 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 08 October 2012 - 03:43 AM

how to make a variable store several values belonging to an array.

How about a parallel 2dim array whose values say what group an element belongs to. The element in the second array would be 'w' if it were a Wall and an 'h' for hollow. Given an x,y pair, index into the second array to see what group the element belongs to.
  • 0

#9 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 08 October 2012 - 03:55 AM

If you mean you need to store a value on this "Grid" as either a hollow or a wall, then you should probably use a container. A container is an object that is used to group a collection of objects, relative to the same object. Inside the container, you could have an integer, called type. Whenever the map loads the grid, depending on the type of the container, it could either be wall, or hollow. This would also allow for future changes to be made easily, as whether it is a wall or a hollow in dependent only on that one integer. For example, you could have this for a container:
public class GameObject {
    private int type;
    public GameObject(int newType){
        type = newType; // Create a new instance of this with type newType
    }
    public boolean isHollow(){ // Is it a hollow?
		 if(type==0){ // If the type is 0, yes, it is a hollow
			 return true;
		 }
		 return false;
    }
    public boolean isWall(){ // Is it a wall?
		 if(type==1){ // If the type is 1, yes, it is a wall
			 return true;
		 }
		 return false;
	 }
}

And then, instead of having a 2 dimensional integer array, you could have a 2 dimensional GameObject array, and whenever you add a GameObject, you would give it an integer value; 0 for Hollow, 0 for Wall. You could also make the integer type of GameObject public, and use just the type integer to iterate through an array. If you wanted it to display graphics, you could iterate through the 2 dimensional GameObject array, and each time you get a type of 0 on checking, it would "spawn" a hollow, and each time you get a type of 1, it would "spawn" a wall.

Keep in mind, the above GameObject is just for example. Personally, I think integer type should be public, and both the get types shouldn't exists. I think that you could just use the type variable.

Using a container allows for future grouping of other variables, for example, if you had an npc, you could also place the the health variable in the container.

Get what I'm saying?
~Chall
  • 0
Speaks fluent Java

#10 kalimann

kalimann

    CC Regular

  • Member
  • PipPipPip
  • 40 posts
  • Learning:Java, C#

Posted 08 October 2012 - 04:40 AM

how to make a variable store several values belonging to an array.

How about a parallel 2dim array whose values say what group an element belongs to. The element in the second array would be 'w' if it were a Wall and an 'h' for hollow. Given an x,y pair, index into the second array to see what group the element belongs to.

Can you gimme an example? Like Grid[1][1] and CheckingGrid[1,1][w] or how?


If you mean you need to store a value on this "Grid" as either a hollow or a wall, then you should probably use a container. A container is an object that is used to group a collection of objects, relative to the same object. Inside the container, you could have an integer, called type. Whenever the map loads the grid, depending on the type of the container, it could either be wall, or hollow. This would also allow for future changes to be made easily, as whether it is a wall or a hollow in dependent only on that one integer. For example, you could have this for a container:

public class GameObject {
private int type;
public GameObject(int newType){
type = newType; // Create a new instance of this with type newType
}
public boolean isHollow(){ // Is it a hollow?
		 if(type==0){ // If the type is 0, yes, it is a hollow
			 return true;
		 }
		 return false;
}
public boolean isWall(){ // Is it a wall?
		 if(type==1){ // If the type is 1, yes, it is a wall
			 return true;
		 }
		 return false;
	 }
}

And then, instead of having a 2 dimensional integer array, you could have a 2 dimensional GameObject array, and whenever you add a GameObject, you would give it an integer value; 0 for Hollow, 0 for Wall. You could also make the integer type of GameObject public, and use just the type integer to iterate through an array. If you wanted it to display graphics, you could iterate through the 2 dimensional GameObject array, and each time you get a type of 0 on checking, it would "spawn" a hollow, and each time you get a type of 1, it would "spawn" a wall.

Keep in mind, the above GameObject is just for example. Personally, I think integer type should be public, and both the get types shouldn't exists. I think that you could just use the type variable.

Using a container allows for future grouping of other variables, for example, if you had an npc, you could also place the the health variable in the container.

Get what I'm saying?
~Chall


Sound like what I already have made:
class Block {
 
   int X, Y, Color;   
   boolean solid;
  
   Block(int posX, int posY, boolean Solid) {
	  X = posX;
	  Y = posY;
	  solid = Solid;
  }

  void draw() {
   if(solid)
	 Color = 255;
   else if(!solid)
	 Color = 0;
   fill(Color);
   rect(X*sqsize, Y*sqsize, sqsize, sqsize);
  }
}

So I should make an array of them? Right now, my thought was to let them take in numbers from an int[][] array which works fine as far as the coloring goes. But I dont know if their values are stored, so that the "Solid" bool can be checked later.
  • 0

#11 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 08 October 2012 - 05:11 AM

If you use objects instead of ints, you can store any information you need about the element at x,y in the object.
  • 0

#12 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 08 October 2012 - 06:20 AM

If you store a value in an array, it will never go away, unless the array changes. You can trust that it will always be the same. You should make an array, and then set the type of them. Don't use color, as you could add color depending on the type of it. Get what I am saying?
  • 1
Speaks fluent Java





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download