Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7177483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:43:24+00:00 2026-05-28T16:43:24+00:00

I am making a 2D array class in Java with wrap around edges. So

  • 0

I am making a 2D array class in Java with wrap around edges. So in an 10×20 array, entry (2,-1) would be the same as (2,19) and (4,22) would be the same as (4,2). I also want to be able to access this structure as a “flattened out array” so I will be using a 1D array-like structure to store the objects. I would use a 1D array, but then I can’t use generic types, so instead I am using an ArrayList. The class isn’t done yet, but I posted the code below.

My first question is algorithmic: In the code below I access elements (i,j) using a modRow(i) and modCol(j) function. It works fine, but I’m wondering if anyone can think of a better way of doing this. These functions are then called by the get(i,j) function. I want to be able to access the elements as fast as possible.

My second question is specific to Java: I am fairly new to Java and am bringing this code over from a project in C++. I have a set() function that allows me to set an element at position i in the array (stored in the ArrayList). However, the set function will only work if the add() function was already called and set the element at position i in the flattened out array. If anyone has any suggestions as to how I could make this class work better I’d be happy to see them.

import java.util.*;

public class Grid<T> {

    ArrayList<T> array; // holds objects in grid - would rather use an array
                        // but that won't support generics
    int rows;  // number of rows in grid
    int cols;  // number of cols in grid
    int length; // total number of objects in grid - maybe just get this from ArrayList
    int conNum; // number of connections for each point on grid (either 4 or 8) 
                // used for determining distances between points on the grid

    // constructor
    Grid(int row, int col, int numCon) {
        rows = row;
        cols = col;
        length = rows * cols;
        conNum = numCon;
        array = new ArrayList<T>(length);
    }

    // returns total size of grid
    public int len() {
        return length;
    }

    // returns number of rows
    public int row() {
        return rows;
    }

    // returns number of columns
    public int col() {
        return cols;
    }

    // wish I didn't have to use this 
    // would be nice to just be able to use set(int i, T t)
    public void add(T t) {
        array.add(t);
    }

    // sets object i in flattened out array
    public void set(int i, T t) {
        array.set(i, t);
    }

    // returns object i in flattened out array
    // for faster access when user just needs to iterate through all objects 
    // in grid without respect to position in 2D grid
    public T get(int i) {
        return array.get(i);
    }

    // returns the row position of i in grid - adjusted for warp around edges 
    // is there a better way to do this?  
    private int modRow(int i) {
        if(i >=0) {
            if(i < rows) {
                return i;
            } else { // i >= rows
                return i % rows;
            }
        } else { // i < 0
            return (i % rows + rows) % rows;
        }
    }

    // returns the column position of j in grid - adjusted for warp around edges 
    // is there a better way to do this? 
    private int modCol(int j) {
        if(j >=0) {
            if(j < cols) {
                return j;
            } else { // j >= cols
                return j % cols;
            }
        } else { // j < 0
            return (j % cols + cols) % cols;
        }
    }

    // sets object at (i,j) value from store adjusted for wrap around edges 
    public void set(int i, int j, T t) {
        array.set(modRow(i) * cols + modCol(j), t);
    }

    // gets object at (i,j) value from store adjusted for wrap around edges 
    public T get(int i, int j) {
        return array.get(modRow(i) * cols + modCol(j));
    }

    // returns distance on the grid between two objects at (y1,x1) and (y2,x2)
    public int dist(int y1, int x1, int y2, int x2) {
        int y = distFirst(y1, y2);
        int x = distSecond(x1, x2);
        if (conNum == 4) // taxicab distance
        {
            return y + x;
        } else { //if(conNum == 8) supremum distance
            return Math.max(y, x);
        }
    }

    // returns distance on the grid between the first coordinates y1 & y2 of two objects
    public int distFirst(int y1, int y2) {
        int dist = Math.abs(modRow(y2) - modRow(y1));
        return Math.min(dist, rows - dist);
    }

    // returns distance on the grid between the second coordinates x1 & x2 of two objects
    public int distSecond(int x1, int x2) {
        int dist = Math.abs(modCol(x2) - modCol(x1));
        return Math.min(dist, cols - dist);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:43:24+00:00Added an answer on May 28, 2026 at 4:43 pm

    Not sure what led you too believe you cannot use generics with arrays, because you can. So change your internal storage definition and call set to your hearts content.

    Your modRows/modCols functions can be (slightly) more efficient. An index will be out of bounds in one direction only so you can get rid of your nested if checks.

    if(index < 0) return index + max_size;
    else if(index >= max_size) return index % max_size;
    else return index;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m making a Shooting game, in that I want an array of ImageViews
Goal I am making a Java class that will give enhanced usability to arrays,
I'm making a game for class and for this game I have an array
I'm making a chess game and I would like to have an array of
Given an unsorted integer array, and without making any assumptions on the numbers in
Making echo of a question around the web: Is the syntax for svn:ignore patterns
I am making a reference to the Javascript function splice() on an array and
I'm making a thread class to use as a wrapper for pthreads. I have
I have a String array that contains names of method in the yyyyyy class
I am working on a collaborative project in Java, and thought it would be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.