I have the following method
public void multiArrayGrid(){
GRect[][] rect = new GRect[3][3];
int rWidth = 50;
int rHeight = 50;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
rect[i][i] = new GRect(50,50);
add(rect[i][i], rWidth+50, rHeight+50);
rWidth+=50;
}
rHeight+=50;
rWidth = 50;
}
}
The above method is actually making a 3x3 grid of rect.
How do I access, for example, rect[0][0]?
The code indeed creates a 3x3 grid, but it fills only the main diagonal (0,0), (1,1), (2,2).
To access rect[0][0] you simply write exactly this expression, and you will get a GRect object or a null pointer. To modify a cell you write the same expression, this time on the left side of an assignment operator.
You did try this yourself, didn't you?
Related
I need to reverse the objects in a 2D Array
It starts with:
{triangle, circle, square}, {star, pentagon, donut}
And should end with:
{square, circle, triangle}, {donut, pentagon, star}
Currently it outputs:
triangle, circle, square, star, pentagon, donut
I have looked at this question but even copying and pasting working code from that question doesn't work.
Here is the current code I have:
Shape[][] myShapes = {{triangle, circle, square}, {star, pentagon, donut}};
public static void reverseShapes(Shape[][] myShapes) {
// TO DO #1: Implement your algorithm to reverse myShapes.
for(int row = 0; row < myShapes.length; row++){
for(int col = 0; col < myShapes[row].length / 2; col++) {
Shape temp = myShapes[row][col];
myShapes[row][col] = myShapes[row][myShapes[row].length - col - 1];
myShapes[row][myShapes[row].length - col - 1] = temp;
}
}
}//end of reverseShapes
This is how I'd write it:
public static void reverseShapes(Shape[][] shapes) {
for(Shape[] row : shapes) {
for(int left=0, right=row.length-1; left<right; left++, right--) {
Shape tmp = row[left];
row[left] = row[right];
row[right] = tmp;
}
}
}
I think it's much easier to understand with the enhanced for loop syntax pulling a row out at a time, and then the indexed for loop using two different variables, "left" and "right" moving inwards from the ends towards the middle without needing to calculate any indices.
I am trying to make a game where the player can only see in a small radius around them. i'm attempting to do this by covering a 500X500 display with 1X1 black pixels that i can set active or inactive. The problem is that using a standered for loop to add them takes a large amount of time when the program launches and it slows the entire thing down. Any Solutions?
the pix object takes two paramaters(int x, int y)
code
public ArrayList<Pix> pixs= new ArrayList<>();
for(int i = 0; i<=500; i++)
{
for(int ii = 0; ii<=500; ii++)
{
pixs.add(new Pix(ii,i));
}
}
You are constructing 250000 instances of your Pix class. That will take some time.
Consider having a 2 dimensional array of booleans instead. Where false means the pixel is black.
You don't need to initialize the values yourself as they will default to false.
boolean[][] pixs = new boolean[500][500];
You can iterate over the structure with this:
for (int x = 0; x < 500; x++) {
for (int y = 0; y < 500; y++) {
System.out.println(pixs[x][y]);
}
}
And you can set a particular pix with
int x = 232;
int y = 455;
pixs[x][y] = true;
import java.util.Random;
public class arrayClass
{
public static void main(String [] args)
{
int [][] array = new int [5][5];
Random gen = new Random();
for(int x = 0; x < array.length; x++)
{
array[x][2]= gen.nextInt(15) + 1;
}
}
}
I know the code is brief but it might be enough for you to understand
Okay so my goal right now is to put random numbers into each cell without "brute forcing" it(so using loops). I was wondering if there is a way to manipulate two variables in a for loop. Also, how could I make it so that the first row will increase one cell when the cells within the row is done in a loop (in this case cell 0 through 4 in one row)
And is there a way to output a specific range of cells?
thanks and sorry I know this might be pretty confusing
I think you really should use a double for loop here:
for (int x=0; x < array.length; x++) {
for (int y=0; y < array[x].length; ++y) {
array[x][y]= gen.nextInt(15) + 1;
}
}
You could use a single for loop to populate the 2D array, but it would require an external loop counter, and in the end would be functionally similar to a double loop.
I have a square board (NxN matrix). Each square (cell) has a certain points associated to it. My goal is to find the largest sub-matrix which has the highest summation of points. I started off with trying to find all the sub-matrices and their weights. But I am stuck on how to go about doing it.
I thought I could have a HashMap<String,Integer> which stores the initial row,column and the size of the sub matrix. The code should look something like this:
int [][] mat = new int[10][10];
void countSubMatrix()
{
for(int i = 0; i<mat.length; i++)
{
for(int j = 0; j<mat[i].length; j++)
{
storeSubMatrix(i,j);
}
}
}
void storeSubMatrix(int x, int y)
{
int size = 0;
int tempX = x;
int tempY = y;
while(tempX < board.length && tempY < board[x].length)
{
map.put(x.toString() + "," + y.toString(),size+1);
tempX++;
tempY++;
}
}
But I don't know if this is the right way to do it. Any thoughts?
Largest submatrix ,i.e, it can also be a rectangle, then this might be of help to you. Using kadane's algorithm for matrix it can be done in O(n^3).
I'm trying to print out an indicator of coordinates on a 2D array...
There is another class that I've used to instantiate objects on the array.
I need to the store coordinates in two (local?) variables, and then display the position of those coordinates in the printed array. (that has already been instantiated with various objects)
This is a snippet of code I have so far, but I can't get the 'C' to print in the right spot on the array. The few options that I tired, either doesn't print it at all, or prints the 'C' in the top left hand corner.
This is one option that I've tired: This option doesn't print the 'C' at all.
private int cX=0;
private int cY=0;
//Randomly set coordinates on array.
for(int i=0; i<array.length; i++){
for(int j=0; j<array[i].length; j++){
int x = randGen.nextInt(9);
int y = randGen.nextInt(9);
if (array [x][y].display()=='.'){
x=cX;
y=cY;
}
}
}
// print array
private void displayArray()
{
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if ((array [i][j].display()==cX)&&
(array [i][j].display()==cY))
System.out.print("C");
System.out.print(board [i][j].display()+"\t")
}
System.out.println("\n");
}
}
If I understood you right, you'd like to save coordinates to some kind of data structure and later display them?
Wouldn't it be suitable to create a class for this purpose? E.g.
A class called Coordinator, that holds the X & Y values.
Later create a object of the class you've made with X & Y values and put it to a ArrayList.
Code Example:
//Creating the a object holding 2 values
Coordinator cords = new Coordinator(randGen.nextInt(9), randGen.nextInt(9));
//Putting in the object to the data structure
List<Coordinator> list = new ArrayList<>();
list.add(cords);
The explanation and code example should help you solve this issue on your own hopefully.
This option doesn't print the 'C' at all.
Your current code example will indeed never print a 'C'.
for (int j = 0; j < array[i].length; j++){
if ((array [i][j].display()==cX)&&
(array [i][j].display()==cY))
This if condition will be true only if cX equals cY and both are equal to the ascii code value assigned to the array (for '.' this is the value 46). But you may just want to find the cX,cY position and print a 'C'. You should try to campare your position (cX, cY) with the current i and j values.
if ((i==cX)&&(j==cY))
prints the 'C' in the top left hand corner
You never change the content of array at the position. You set x and y which will be overwritten and are later no accessible
int x = randGen.nextInt(9);
int y = randGen.nextInt(9);
if (array [x][y].display()=='.'){
x=cX;
y=cY;
}
Do you want to find a random position for your 'C' to appear, like in a board game?
Then you need to assign your found x and y coordinate to cX and cY. The right value gets assigned to the left variable.
cX = x;
cY = y;
I hope I understood you right and this helps.
For completeness: I think I worked it out. (Well it seems to be working anyway).
Thanks for the help!
private int cX=0;
private int cY=0;
//Randomly set coordinates on the array.
for(int i=0; i<1; i++){
int x = randGen.nextInt(9);
int y = randGen.nextInt(9);
if (board [x][y].display()=='.'){
CX=x;
CY=y;
}
}
//Print array
private void displayArray(){
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[i].length; j++){
if ((i==CX)&&(j==CY))
System.out.print("C"+"\t");
else
System.out.print(board [i][j].display()+"\t");
}
System.out.println("\n");
}
}
private double[][] mLatLng = {
{19.01062463, 73.01141475},
{19.02369039, 73.00778391},};
public void onMapReady(GoogleMap googleMap) { googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_icon);
for (int i = 0; i < mLatLng.length; i++) {
for (int j = 0; j < mLatLng[i].length; j++) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(mLatLng[i][j], mLatLng[i][j]))
.title("Location" + i).icon(bitmapDescriptorFromVector(context,R.drawable.ic_icon)));