正如您将看到的,我对面向对象编程相当新。我正在努力教自己,但我坚持这一点,无法弄清楚该怎么做。
我正在尝试编写一些代码来将矩形网格布局为行和列。想想“将小方块放在一个大矩形上”。用户将输入他们拥有的“小方块”的数量。我的目标是将此整数映射到行和列的最佳布局。
用户可以输入20到100之间的任何整数。对于81个不同的可能条目中的每一个,我已经确定了在行和列中布局这些小方块的最佳方法。我现在需要做的是将这81种不同的布局放入我的程序中,然后识别并返回适用于用户输入的布局。
由于只有81个值并且它们的范围是连续的整数,我认为数组是最好的主意(而不是地图,散列图,矢量等)。这是我到目前为止所拥有的。这有点乱,但我想你会明白我想做什么。谁能帮我?我似乎无法返回我需要的行和列值。
谢谢!
public static void getLayout (int numSquares) {
int rows;
int columns;
Layouts myLayout = new Layouts();
rows = myLayout[numSquares].r; //this is where it fails
columns = myLayout[numSquares].c;
}
class RowCol<R, C> {
/* Class Variables */
public final R r;
public final C c;
public RowCol(R r, C c) {
this.r = r;
this.c = c;
}
public R getRow() {return r;}
public C getCol() {return c;}
}
class Layouts {
RowCol[] myLayouts;
public Layouts() {
/* Set the numbers of Rows and Columns for each possible
* number of squares requested.
*/
myLayouts[20] = new RowCol(5, 4); // 20 Problems
myLayouts[21] = new RowCol(7, 3); // 21 Problems
myLayouts[22] = new RowCol(5, 4); // 22 Problems
myLayouts[23] = new RowCol(5, 4); // 23 Problems
myLayouts[24] = new RowCol(6, 4); // 24 Problems
myLayouts[25] = new RowCol(5, 5); // 25 Problems
myLayouts[26] = new RowCol(6, 4); // 26 Problems
myLayouts[27] = new RowCol(6, 4); // 27 Problems
myLayouts[28] = new RowCol(7, 4); // 28 Problems
//etc...
编辑:应用下面的响应,我需要在Layouts类中实例化一个对象,我做了。然后,我修改了getLayout类以隔离返回的RowCol值。这是更新的课程。我在这一点上的问题是我无法将行和列转换为整数,以便我可以这样使用它们。
public static void getLayout(int numSquares) {
Layouts myLayout = new Layouts();
RowCol myRC = myLayout.getLayout(numProbs);
int rows = Integer.parseInt(myRC.getRow());
int cols = Integer.parseInt(myRC.getCol());
}
错误是:
编辑:解决了。谢谢大家!找不到适合parseInt(Object)的方法 方法Integer.parseInt(String)不适用 (实际参数Object不能通过方法调用转换转换为String) 方法Integer.parseInt(String,int)不适用 (实际和正式的参数列表长度不同)
答案 0 :(得分:3)
我可以看到一个快速的问题。 Layouts类不支持任何方式的基于索引的访问。所以下面的代码会抛出一个错误:
rows = myLayout[numSquares].r;
您可能希望在Layouts类中创建一个方法,该方法返回其私有数组myLayouts,它还可以与索引一起使用。
而且,正如DaoWen 7指出的那样,该阵列需要在某处初始化。
我是通过手机写的,所以输入的内容不多:-P
答案 1 :(得分:2)
当你混合数组和泛型时,由于类型擦除会发生奇怪的事情。
我可能不会将RowCol
作为通用类,只需使用int
作为字段类型。
答案 2 :(得分:1)
您是否初始化了阵列?
RowCol[] myLayouts = new RowCol[81];
如果您没有初始化数组,则myLayouts
引用为null
,当您尝试分配或读取任何索引时,您将获得NullPointerException
。
<强>更新
您遇到parseInt
时遇到的问题源于您在阵列中使用原始类型的泛型。您在RowCol
类的声明中有类型参数(class RowCol<R, C>
具有类型参数R
和C
),但是当您声明{的实例时,您没有指定类型{1}},所以他们只是默认为原始类型,假设一切都是RowCol
。这应该解决它:
Object
由于Java对泛型和数组的限制,上面将生成警告,但@SuppressWarnings("unchecked")
RowCol<Integer,Integer>[] myLayouts = new RowCol[81];
// . . .
myLayouts[20] = new RowCol<Integer,Integer>(5, 4); // 20 Problems
myLayouts[21] = new RowCol<Integer,Integer>(7, 3); // 21 Problems
// . . .
注释应该处理它。您也可以使用@SuppressWarnings
(具有完全通用支持)而不是数组来完全避免此问题。
答案 3 :(得分:0)
请在下面
public static void getLayout (int numSquares) {
int rows;
int columns;
//Create object here currently if i create it then it will be my class specific
Layouts myLayout = new Layouts();
rows =myLayout.myLayouts[numSquares].r; //this is where it fails
columns = myLayout.myLayouts[numSquares].c;
}
class RowCol<R, C> {
/* Class Variables */
public final R r;
public final C c;
public RowCol(R r, C c) {
this.r = r;
this.c = c;
}
public R getRow() {return r;}
public C getCol() {return c;}
}
class Layouts {
RowCol<Integer, Integer>[] myLayouts = new RowCol[81];
public Layouts() {
/* Set the numbers of Rows and Columns for each possible
* number of squares requested.
*/
myLayouts[20] = new RowCol<Integer, Integer>(5, 4); // 20 Problems
myLayouts[21] = new RowCol<Integer, Integer>(7, 3); // 21 Problems
myLayouts[22] = new RowCol<Integer, Integer>(5, 4); // 22 Problems
myLayouts[23] = new RowCol<Integer, Integer>(5, 4); // 23 Problems
myLayouts[24] = new RowCol<Integer, Integer>(6, 4); // 24 Problems
myLayouts[25] = new RowCol<Integer, Integer>(5, 5); // 25 Problems
myLayouts[26] = new RowCol<Integer, Integer>(6, 4); // 26 Problems
myLayouts[27] = new RowCol<Integer, Integer>(6, 4); // 27 Problems
myLayouts[28] = new RowCol<Integer, Integer>(7, 4); // 28 Problems
//etc...
}
}