假设我有这些类:与多个阵列工作,并增加对象
public class EdgeI {
public int from;
public int to;
public EdgeI (int a1, int a2) {
from = a1;
to = a2;
}
}
public class VertexI {
public List neighbors;
public String info;
public VertexI (List neig, String str) {
neighbors = neig;
info = str;
}
}
public class vertexWeight {
public int v;
public int w;
public vertexWeight (int vertexNum, int wum) {
v = vertexNum;
w = wum;
}
}
假设我有包含对数字的EdgeI
对象的列表。假设我也有一个包含空列表和字符串的VertexI
对象列表。我想补充以下的空单:
假设我有这个作为我EdgeI的对象列表
(1,2), (1,2) (1,2), (1,3), (1,3), (1,4)
对于列表中的第一个VertexI
对象,我想补充以下列表
(2,3) (3,2)
到顶点对象。基本上我想取“to”整数以及“to”整数重复的次数,并创建vertexWeight
对象,将其添加到VertexI
类的neig
列表中。因此neig
为第一个VertexI
对象将是vertexWeight
对象(2,3)
和(3,2)
。为了实现这个,我创建了这个到目前为止:
public void createGraph() {
int oldFrom = -1;
int oldTo = -1;
for(int i = 0; i < edges.size(); i++) {
EdgeI e = edges.get(i);
int from = e.from;
int to = e.to;
VertexI v = vertices.get(from);
v.neighbors.add(new vertexWeight (to, 1));
if (from == oldFrom && to == oldTo){}
//have to add increment the number 1 in the vertex weight object somehow
else {
oldFrom = from;
oldTo = to;
}
}
}
我需要一些提示或方法来实现这个?我的逻辑可能不正确,那是我需要最多帮助的地方。
尽量避免像您的第一个代码示例一样的“假”代码。如果你想简洁,你可以不用列出语法错误的构造函数签名,而是包含'public'或默认访问字段,并用'// ...'缩写构造函数体。 (一个好主意,如果构造函数可以由IDE生成)。这样,或多或少地清楚你的类的结构是什么,并且代码将在复制粘贴后编译,因此需要尝试较少的修改回答。 – millimoose
修复了小费的提示 – ellangog