I have this issue with the xlabels in aChartEngine when there is one value in an ArrayList. I generate my own XLabels for a line chart, but when there is one data array value, i get an unwanted TimeStamp on top of my generated XLabels.
my Code:
private XYMultipleSeriesDataset getDemoDataset() {
Calendar cal = Calendar.getInstance();
Date date = new Date();
date_value = new String[list.size()];
value_value = new String[list.size()];
for(int k = 0; k < list.size(); k++){
date_value[k] = list.get(k).get(DATE);
value_value[k] = list.get(k).get(VALUE);
}
TimeSeries series = new TimeSeries("Line Graph");
for(int j=0; j < date_value.length; j++) {
series.add(formatter.stringToDate(date_value[j]), Integer.parseInt());
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
return dataset;
}
setChartSetting:
private void setChartSetting(XYMultipleSeriesRenderer renderer){
renderer.setXRoundedLabels(true);
renderer.setXTitle("DATES");
renderer.setYTitle(kpiname);
renderer.setApplyBackgroundColor(true);
renderer.setFitLegend(false);
renderer.setAxesColor(Color.DKGRAY);
renderer.setShowGrid(true);
renderer.setZoomEnabled(false);
renderer.setXLabels(0);
renderer.setYLabels(10);
renderer.setZoomEnabled(false, false);
renderer.setPanEnabled(false, false);
//String[] date_value = new String[list.size()];
//String[] value_value = new String[list.size()];
if(list.isEmpty() || list.size() == 0 || list == null ){
return;
}
if(name.equals(RECOVERED) || name.equals(CONVERSION)){
largest_size = (int)Double.parseDouble(value_value[0]);
}else{
if(!(value_value.length == 0)){
largest_size = Integer.parseInt(value_value[0]);
}else{
return;
}
}
//used to determine the maximum value for the y-axis
for(int x =0; x < value_value.length; x++){
if(Integer.parseInt(value_value[x]) > largest_size){
largest_size = Integer.parseInt(value_value[x]);
}
}
renderer.setYAxisMax((double)largest_size);
renderer.setYAxisMin(0);
int value_size = value_value.length;
int m = 0;
//int add = value_size/10;
int add = largest_size/10;
/*instance variables for setting the labels on the X-Axis based on weather the values in the array are
* more than 10 items or less*/
double d;
Long l;
Long l1;
double d1;
if(date_value.length <= 1){
int last = date_value.length;
int mod = 0;
//int add_mod = Math.round(last/10);
int add_mod = (int)Math.round((double)Math.round(last));
l = Long.valueOf(formatter.stringToDateReport(date_value[0]).getTime());
d = l.doubleValue();
/*tried this to see if i can remove the timestamp*/
//renderer.addXTextLabel(0.0, "");
//renderer.removeXTextLabel(0.0);
//renderer.clearXTextLabels();
renderer.addXTextLabel(d, date_value[0]);
}
else if(date_value.length < 10){
for(int i = 0; i < date_value.length; i++){
if(i >= date_value.length){
break;
}
l = Long.valueOf(formatter.stringToDateReport(date_value[i]).getTime());
d = l.doubleValue();
renderer.addXTextLabel(d, date_value[i]);
}
} else if(date_value.length >= 10){
int last = date_value.length;
//int last = 28;
//Log.d(TAG, "last is " + last);
int mod = 0;
//int add_mod = Math.round(last/10);
int add_mod = (int)Math.round((double)Math.round(last) /10); // do this to get the decimal value with a double and then round to get nearset whole with int
for(int i =0; i < date_value.length; i++){
if(mod >= date_value.length){
break;
}
l1 = Long.valueOf(formatter.stringToDateReport(date_value[mod]).getTime());
d1 = l1.doubleValue();
renderer.addXTextLabel(d1, date_value[mod]);
mod+=add_mod;
}
}
}
}
and then:
chartView = ChartFactory.getLineChartView(context, getDemoDataset(), setChartSettings(renderer));
When there are multiple values, it plots the graph well with my custom XLabels(format is MMM-yyyy), but when there is only one value in the ArrayList, it generates thats time stamp.
here is an image:
This was a bug, indeed. You can download a version including a fix for it feature here.
Related
I want to generate n no of random no whose sum should be m and these no should always be between x and y.
For eg :- If i say I need 20 no, between 3 and 9 inclusive, whose sum should be 100.
I want to specify the sum and required amount of no as well as the limit between which random no should be generated and it should give me the random no of specified amount. Below is my code which do generates no which are greater than 2 but i am not able to put the max limit. Also, the sum which comes out and the sum of no. which it generates is not equal to specified sum. I am totally confused Please help me out.
TextView textView,randomTotaltxt;
int count0 = 0;
int count1 = 0;
int targetSum = 100;
int numberofDraws = 20;
textView = (TextView) findViewById(R.id.textview);
randomTotaltxt = (TextView) findViewById(R.id.randomTptalText);
Random r = new Random();
ArrayList<Integer> load = new ArrayList<>();
//random no
int sum = 0;
for (int i=0; i<numberofDraws;i++){
int next = r.nextInt(targetSum)+1;
Log.d("No",""+next);
load.add(next);
sum += next;
}
//scale it
double scale = 1d*targetSum/sum;
sum=0;
for (int i=0; i<numberofDraws;i++){
load.set(i, (int)(load.get(i)*scale));
sum += load.get(i);
}
while (load.contains(0)|| load.contains(1)){
for (int i=0; i<load.size();i++) {
if (load.get(i).equals(0)) {
load.set(i, load.get(i) + 2);
count0++;
sum = sum+1;
}
if (load.get(i).equals(1)) {
load.set(i, load.get(i) + 2);
sum += load.get(i);
count1++;
sum = sum+1;
}
}
}
// take rounding
while (sum++ <targetSum){
int i = r.nextInt(numberofDraws);
load.set(i, load.get(i) + 1);
}
textView.setText(""+load);
randomTotaltxt.setText(""+(sum-1));
This might not be the optimal solution but this will do your work as required.
int targetSum = 100;
int numberOfDraws = 20;
int uppr=0;
int countt =0;
ArrayList<Integer> load = new ArrayList<>();
Random random;
TextView textView1,randomTotaltxt1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView1 = (TextView) findViewById(R.id.textview1);
randomTotaltxt1 = (TextView) findViewById(R.id.randomTptalText1);
random = new Random();
//random no
int sum = 0;
for (int i=0; i<numberOfDraws;i++){
int next = random.nextInt(targetSum)+1;
load.add(next);
sum += next;
}
//scale it
double scale = 1d*targetSum/sum;
sum=0;
for (int i=0; i<numberOfDraws;i++){
load.set(i, (int)(load.get(i)*scale));
sum += load.get(i);
}
// take rounding
while (sum++ <targetSum){
int i = random.nextInt(numberOfDraws);
load.set(i, load.get(i) + 1);
Log.d("TAG",""+load);
}
checkForUpperRange();
}
public void checkForUpperRange(){
for (int i=0; i<numberOfDraws;i++){
int n = load.get(i);
if (n > 9){
load.set(i, 9);
uppr = n - 9; // getting the required no.
adjustUpper();
break;
}
checkForLowerRange();
}
}
private void adjustUpper() {
for (int j=0; j<numberOfDraws;j++){
if (load.get(j) > 2){
load.set(j, load.get(j)+uppr); // uppr is added
checkForUpperRange();
break;
}
}
}
public void checkForLowerRange(){
for (int i=0; i<numberOfDraws;i++){
int n = load.get(i);
if (n == 0){
load.set(i, load.get(i)+3); // 3 is added in sum
adjust0();
break;
}
if (n == 1){
load.set(i, load.get(i)+2); // 2 is added in sum
adjust1();
break;
}
if (n == 2){
load.set(i, load.get(i)+1); // 1 is added in sum
adjust2();
break;
}
getCount();
}
}
public void getCount(){
countt = 0;
for (int k=0; k<numberOfDraws;k++){
countt = countt+ load.get(k);
showResult();
}
}
public void showResult(){
textView1.setText(""+load);
randomTotaltxt1.setText(""+countt);
}
public void adjust0(){
for (int j=0; j<numberOfDraws;j++){
if (load.get(j) > 3){
load.set(j, load.get(j)-3); // 3 is subtracted
checkForLowerRange();
break;
}
}
}
public void adjust1(){
for (int j=0; j<numberOfDraws;j++){
if (load.get(j) > 3){
load.set(j, load.get(j)-2); // 2 is subtracted
checkForLowerRange();
break;
}
}
}
public void adjust2(){
for (int j=0; j<numberOfDraws;j++){
if (load.get(j) > 3){
load.set(j, load.get(j)-1); // 1 is subtracted
checkForLowerRange();
break;
}
}
}
The following is my code but it doesn't generate unique random numbers
Random rand = new Random();
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = rand.nextInt(n)+1;
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
public TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView);
Random r = new Random();
StringBuffer temp =new StringBuffer("Random numbers:");
for(int i=0;i<10;i++) {
int i1 = r.nextInt(100 - 0) + 0;
temp.append(String.valueOf(i1));
temp.append(String.valueOf(" "));
}
tv.setText(temp.toString());
}
//here ,,I generate 10 random numbers and save it in to stringBuffer and dispaly it in to textView..here range is up to 0 to 100...you can take your own Range ...I hope ,,It will help you...
check this out!... it works for me
Random rand = new Random();
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = (int) (rand.nextDouble() * n + 1);
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int piece: array) {
System.out.println(piece);
}
I'm writing code that performs a projective transform (https://math.stackexchange.com/questions/296794/finding-the-transform-matrix-from-4-projected-points-with-javascript) on an image using 4 user selected points.
In doing so I have to use very large arrays (300k+ indices). When I run it, my phone screen blacks out and after a few seconds gives the message " has stopped working." However, it continues to print Log messages to my AndroidStudio logcat containing information about the array that it's working on, letting me know that it's still running.
I'm not very knowledgeable about computational efficiency, so I might be making some fatal mistake involving matrix manipulation. The part of the code that it breaks on is the final portion of transform(), and the logcat prints the "rounded" values while the phone shows a "stopped working" message.
I've included the relevant code. Any advice on anything I'm doing wrong (related or not) is appreciated as this is my first experience with Android development.
I'm more or less just following the transformation provided by in the math.stackexchange link.
public class projTransform extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_proj_transform);
Intent parent_intent = getIntent();
Uri imgUri = parent_intent.getData();
pointArray = parent_intent.getDoubleArrayExtra("points");
//dimens[0-3]: width, height, minX, minY
dimens = parent_intent.getIntArrayExtra("dimens");
transform(imgUri,pointArray, dimens);
}
//A*B = C
private static double[][] mMult(double[][] A, double[][] B){
int mA = A.length;
int nA = A[0].length;
int mB = B.length;
int nB = B[0].length;
if (nA != mB) throw new RuntimeException("Illegal matrix dimensions.");
double[][] C = new double[mA][nB];
for (int i = 0; i < mA; i++)
for (int j = 0; j < nB; j++)
for (int k = 0; k < nA; k++)
C[i][j] += A[i][k] * B[k][j];
return C;
}
//A*x = y
private static double[] mMult(double[][] A, double[] x){
int m = A.length;
int n = A[0].length;
if (x.length != n) throw new RuntimeException("Illegal matrix dimensions.");
double[] y = new double[m];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
y[i] += A[i][j] * x[j];
return y;
}
//https://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_3.C3.973_matrices
//A^(-1)
private static double[][] mInvert3x3(double[][] X){
double[][] Y = new double[3][3];
double A,B,C,D,E,F,G,H,I,detX;
A = X[1][1]*X[2][2] - X[1][2]*X[2][1];
B = -(X[1][0]*X[2][2] - X[1][2]*X[2][0]);
C = X[1][0]*X[2][1] - X[1][1]*X[2][0];
D = -(X[0][1]*X[2][2] - X[0][2]*X[2][1]);
E = X[0][0]*X[2][2] - X[0][2]*X[2][0];
F = -(X[0][0]*X[2][1] - X[0][1]*X[2][0]);
G = X[0][1]*X[1][2] - X[0][2]*X[1][1];
H = -(X[0][0]*X[1][2] - X[0][2]*X[1][0]);
I = X[0][0]*X[1][1] - X[0][1]*X[1][0];
detX = X[0][0]*A + X[0][1]*B + X[0][2]*C;
Y[0][0] = A/detX;
Y[1][0] = B/detX;
Y[2][0] = C/detX;
Y[0][1] = D/detX;
Y[1][1] = E/detX;
Y[2][1] = F/detX;
Y[0][2] = G/detX;
Y[1][2] = H/detX;
Y[2][2] = I/detX;
return Y;
}
private void transform(Uri data, double[] sourceArray, int[] dimens){
if (data != null) {
try {
InputStream imgStream = getContentResolver().openInputStream(data);
tempBmp = BitmapFactory.decodeStream(imgStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedbmp = Bitmap.createBitmap(tempBmp, 0, 0, tempBmp.getWidth(), tempBmp.getHeight(), matrix, true);
crop = new int[dimens[0] * dimens[1]];
rotatedbmp.getPixels(crop, 0, dimens[0], dimens[2], dimens[3], dimens[0], dimens[1]);
//map for original bmp
double[][] sourceMap = tMap(sourceArray);
Log.e("sourceMap",toString(sourceMap));
//map for transformed bmp
double[] destArray = new double[] {0,0,0,destHeight,destWidth,0,destHeight,destWidth};
double[][] destMap = tMap(destArray);
Log.e("destMap",toString(destMap));
// C = B*[A^(-1)]
double[][] finalMap = mMult(sourceMap, mInvert3x3(destMap));
Log.e("width", String.valueOf(dimens[0]));
int[] destPixels = new int[destHeight*destWidth];
int[] temp;
for(int i=0; i<destHeight-1; i++){
for(int j=0; j<destWidth-1; j++){
temp = pixelMap(finalMap,i,j);
Log.e("rounded", String.valueOf(temp[0]) + ", " + String.valueOf(temp[1]));
destPixels[(i*destWidth)+j] = crop[(temp[0]*dimens[0]) + temp[1]];
}
}
display(destPixels, destWidth, destHeight);
}
}
//produces mapping matrix given corners
//A,B in SE post
private double[][] tMap(double[] pointArray){
double[][] tempArray = new double[3][3];
tempArray[0][0] = pointArray[0];
tempArray[1][0] = pointArray[1];
tempArray[0][1] = pointArray[2];
tempArray[1][1] = pointArray[3];
tempArray[0][2] = pointArray[4];
tempArray[1][2] = pointArray[5];
for(int i=0; i<3; i++){
tempArray[2][i] = 1;
}
//Log.e("tempArray",toString(tempArray));
double[] tempVector = new double[] {pointArray[6], pointArray[7], 1};
//Log.e("tempVector",toString(tempVector));
double[][] inverted = mInvert3x3(tempArray);
//Log.e("inverted",toString(inverted));
double[] coef = mMult(inverted, tempVector);
//Log.e("coef",toString(coef));
double[][] tran = new double[3][3];
for(int i=0; i<3; i++){
for (int j=0; j<3; j++){
tran[i][j] = tempArray[i][j]*coef[j];
}
}
return tran;
}
private int[] pixelMap(double[][] map, double x, double y){
double[] tempVector = new double[] {x,y,1};
double[] primeVector = mMult(map,tempVector);
return new int[] {(int) Math.round(primeVector[0]/primeVector[2]), (int) Math.round(primeVector[1]/primeVector[2])};
}
You don't need to divide by the determinant; the adjoint instead of the inverse is sufficient. Apart form that, you're creating a lot of objects.
The tight loop is one with the two nested loops around the pixelMap call. Try to inline pixelMap there, and try to avoid creating new arrays. Use separate variables for all the infividual coordinates. Use the fact that you know the dimensions.
x = finalMap[0][0]*i + finalMap[0][1]*j + finalMap[0][2];
and so on. If you want to, you can move finalMap into a set of 9 local variables to help the optimizer. Also disable the logging line, since caching and transfering that much log output takes considerable resources. Do one log line when you start the loop, and another when you're done. In the end, the loop would look somewhat like this:
for(int i=0; i<destHeight-1; i++){
for(int j=0; j<destWidth-1; j++){
double x = finalMap00*i + finalMap01*j + finalMap02;
double y = finalMap10*i + finalMap11*j + finalMap12;
double z = finalMap20*i + finalMap21*j + finalMap22;
int xi = (int)Math.round(x/z), yi = (int)Math.round(y/z);
destPixels[(i*destWidth)+j] = crop[(xi*srcWidth) + yi];
}
}
public static ArrayList<double[]> Value = new ArrayList<double[]>();
private double[] x = new double[10];
private double[] y = new double[10];
int counter = -1;
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
counter++;
x[counter] = Double.parseDouble(income_1.getText().toString());
y[counter] = Double.parseDouble(income_2.getText().toString());
income_1.setText("");
income_2.setText("");
}
});
publish.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (Value != null) {
Value.add(x);
Value.add(y);
Intent intent = salesStackedBarChart.execute(BarChart.this,
Value, counter);
startActivity(intent);
}
}
});
//and in SalesStackedBarChart.java class
public Intent execute(Context context, ArrayList<double[]> values ,int counter) {
int count = counter + 1;
double fcount = counter + 1.5;
String[] titles = new String[] { "Android", "iPhone" };
int[] colors = new int[] { Color.GREEN, Color.CYAN };
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
setChartSettings(renderer, "Yearly revenue in the last "+count+" years", "Years", "revenue in $", 0.5,
fcount, 0, 24000, Color.GRAY, Color.LTGRAY);
renderer.setXLabels(count);
renderer.setYLabels(10);
renderer.setDisplayChartValues(true);
renderer.setXLabelsAlign(Align.LEFT);
renderer.setYLabelsAlign(Align.LEFT);
renderer.setZoomRate(1.1f);
renderer.setBarSpacing(0.5);
return ChartFactory.getBarChartIntent(context, buildBarDataset(titles, values), renderer,
Type.DEFAULT);
}
// in AbstractDemoChart.java class
protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++) {
CategorySeries series = new CategorySeries(titles[i]);
double[] v = values.get(i);
int seriesLength = v.length;
for (int k = 0; k < seriesLength; k++) {
series.add(v[k]);
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
Run this project i get graph with x- axis value:
1,2,3,4,5....
But I want to print value:
2005,2006,2007,2008.....
I changed in some code like:
setChartSettings(renderer, "Yearly revenue in the last "+count+" years", "Years", "revenue in $", 2005,
2010, 0, 24000, Color.GRAY, Color.LTGRAY);
and run project i get value of x-axis like:
2005,2006,2007....
but not get graph bar value. Values of all x-axis are null. How can I make this work?
public static ArrayList<double[]> Value = new ArrayList<double[]>();
private double[] x = new double[2010];
private double[] y = new double[2010];
int counter = 2005;
i have done it this way:
xScale = 5;
Calendar today = Calendar.getInstance();
for (int i = 0; i < this.xScale; i++) {
Calendar c = (Calendar) today.clone();
c.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE) - (this.xScale - (i + 1)));
x.get(0)[i] = new Date(c.get(Calendar.YEAR) - 1900, c.get(Calendar.MONTH), c.get(Calendar.DATE));
x.get(1)[i] = new Date(c.get(Calendar.YEAR) - 1900, c.get(Calendar.MONTH), c.get(Calendar.DATE));
}
this shows you the last 5 days an the x axis
I have been killing myself trying to develop an algorithm to determine the positions of the cells in a grid by which row or column they are in.
For example, I have an 8x8 grid and I need to determine all of the positions of the cells in row 3.
The issue that I have is that, without alot of loops, I need to figure out a way to map the position clicked to the row and column that contain that cell.
Here is what I did to create an ArrayList of the cells:
public class GameBoard {
public ArrayList<Row> rows = new ArrayList<Row>();
public ArrayList<Column> columns = new ArrayList<Column>();
private int maxPositions;
public GameBoard(int rowCount, int colCount) {
this.maxPositions = (rowCount * colCount) - 1;
for(int x = 0;x < rowCount;x++){
Row row = new Row(x);
row.cells.addAll(this.getRowCells(x, colCount));
rows.add(row);
}
for(int x = 0;x < colCount;x++){
Column column = new Column(x);
column.cells.addAll(getColumnCells(x, colCount));
columns.add(column);
}
}
private ArrayList<Cell> getRowCells(int rowId, int colCount) {
ArrayList<Cell> row_cells = new ArrayList<Cell>();
int first_position = ((rowId * colCount) -1);
Cell cell = new Cell();
cell.id = first_position;
row_cells.add(cell);
int x = 1;
while(x < colCount) {
cell = new Cell();
cell.id = first_position + x;
row_cells.add(cell);
x++;
}
return row_cells;
}
private ArrayList<Cell> getColumnCells(int columnId, int rowCount) {
ArrayList<Cell> col_cells = new ArrayList<Cell>();
int first_position = columnId;
Cell cell = new Cell();
cell.id = first_position;
col_cells.add(cell);
int x = 1;
while(x < rowCount) {
cell = new Cell();
cell.id = rows.get(x).cells.get(columnId).id;
col_cells.add(cell);
x++;
}
return col_cells;
}
public Cell getColumnNextCell(int position, int columnId) {
Cell cell = null;
if ((position > -1) && (position <= maxPositions)) {
cell = columns.get(columnId).cells.get(position + 1);
}
return cell;
}
public Cell getColumnPreviousCell(int position, int columnId) {
Cell cell = null;
if ((position > 0) && (position <= maxPositions)) {
cell = columns.get(columnId).cells.get(position - 1);
}
return cell;
}
public Cell getRowNextCell(int position, int rowId) {
Cell cell = null;
if ((position > -1) && (position <= maxPositions)) {
cell = rows.get(rowId).cells.get(position + 1);
}
return cell;
}
public Cell getRowPreviousCell(int position, int rowId) {
Cell cell = null;
if ((position > 0) && (position <= maxPositions)) {
cell = rows.get(rowId).cells.get(position - 1);
}
return cell;
}
}
As you can see with the methods like getColumnNextCell() through getRowPreviousCell() the logic is a mess and I am stuck on how to determine the column and row of the clicked cell (as I explained). Any help would be greatly appreciated.
For clarification the Row and Column objects are identical. They look like this.
public class Column {
public int id;
public ArrayList<Cell> cells = new ArrayList<Cell>();
public Column(int idx) {
this.id = idx;
}
}
Can't you just use an array for the cells
int rows, cols;
Cell[][] cells;
public GameBoard(int rowCount, int colCount) {
this.maxPositions = (rowCount * colCount) - 1;
this.rows = rowCount;
this.cols = colCount;
this.cells = new Cell[rows];
for(int i=0; i < rows; i++) {
cells[i] = new Cell[cols];
for(int j = 0; j < cols; j++) {
cells[i][j] = new Cell();
// whatever else you need to do...
}
}
}
This way it would be easy to get the cell you want
Cell getCell(int position) {
return cells[position/rows][position%cols];
}
Why don't you use simple two dimensional array? If your grid is not resized after creation, there is no advantage of using ArrayList objects. So what about having your base class something like this:
public class GameBoard {
private Cell[][] cells;
private int maxPositions;
private int rows;
private int cols;
public GameBoard(int rowCount, int colCount) {
this.rows = rowCount;
this.cols = colCount;
cells = new Cell[rowCount][colCount];
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
cells[i][j] = new Cell();
}
}
this.maxPositions = (rowCount * colCount) - 1;
}
...
}
Now you don't have row and column objects, just array of cells. Isn't those cells actually what you need or do those row and column arrays have another purpose? So I think you are trying to do simple thing with too difficult way.