java中带链接和链接权重的二维数组

java中带链接和链接权重的二维数组,java,arrays,matrix,junit,nodes,Java,Arrays,Matrix,Junit,Nodes,我正在进行一个页面排名案例研究,其中输入首先是节点数,然后是每个页面I到页面j的链接。整个输入必须根据行和空格进行拆分(因为输入是一个由多行整数组成的大字符串)。我想要实现的测试是:getNodeSize,它只返回节点,hasLink(整数I,整数j)为布尔值,无论节点I到节点j(从0到节点-1计数)是否存在链接,以及一个方法linkWeight(整数I,整数j),它返回整数I和j之间的链接数。(之后还有一些其他的方法,但我会在稍后找出) 到目前为止,我的代码是: public class We

我正在进行一个页面排名案例研究,其中输入首先是节点数,然后是每个页面I到页面j的链接。整个输入必须根据行和空格进行拆分(因为输入是一个由多行整数组成的大字符串)。我想要实现的测试是:getNodeSize,它只返回节点,hasLink(整数I,整数j)为布尔值,无论节点I到节点j(从0到节点-1计数)是否存在链接,以及一个方法linkWeight(整数I,整数j),它返回整数I和j之间的链接数。(之后还有一些其他的方法,但我会在稍后找出) 到目前为止,我的代码是:

public class WebGraph {
        static Scanner s = new Scanner(System.in);
        static int nodes = s.nextInt();
        double wg [][]= new double [nodes][nodes];
        static ArrayList<String> array = new ArrayList<String>();
        static int[][] counts = new int[nodes][nodes];    
        static int[] outDegree = new int[nodes];

public static WebGraph initFromScanner(Scanner s) {  

    do{
        for(int i = 0; i<nodes-1; i++){
    }
        String[] strArray = s.nextLine().split("\\s+");
        int[] intArray = new int[strArray.length];
        for(int i = 0; i < strArray.length; i++) {
        intArray[i] = Integer.parseInt(strArray[i]);
        }       
    }while(s.hasNext());
    return null;
}

public Object getNodeSize() {
    nodes = s.nextInt();
    return nodes;
}
public boolean hasLink(int i, int j) {
    for (int h = 0; h<wg[i].length; h++) {
        if(h==j){
            return true;
        }
    }
    return false;
}

public void linkWeight(int a, int b) {
    while (s.hasNext())  {
        int i = s.nextInt(); 
        int j = s.nextInt(); 
        outDegree[i]++; 
        counts[i][j]++; 
}
}
public List<Integer> successorsSortedList(int i) {
    // TODO Auto-generated method stub
    return null;
}
public int[][] asMatrix() {

    return null;
}
public Set<Integer> successorsSortedSet(int i) {

    return null;
}
}
公共类WebGraph{
静态扫描仪s=新扫描仪(System.in);
静态int节点=s.nextInt();
double wg[][]=新的双[节点][节点];
静态ArrayList数组=新ArrayList();
静态int[][]计数=新int[节点][节点];
静态int[]outDegree=新int[节点];
公共静态WebGraph initFromScanner(扫描仪s){
做{

对于(int i=0;istatic方法initFromScanner不修改其类的任何静态变量,并返回null。因此,它所做的只是将一些数据读入位桶中。这符合计划吗?由于initFromScanner返回null,显然依赖于类WebGraph的对象的任何测试都无法工作。方法hasLink使用循环检查参数j是否是从0到wg[i]的值之一。长度-1。只需将j与0和其他值进行比较即可检查。-可能你是指其他值吗?方法getNodeSize看起来像“getter”。当它读取和设置实例变量时,它实际上是“getter”和“setter”,这将混淆该类的所有用户并引入错误。方法linkWeight(int a,int b)有两个参数,两个参数都没有使用。您编写它应该返回一些内容(测试尝试匹配其返回值)但是,唉,这是一个无效的方法。静态方法initFromScanner不修改其类的任何静态变量,它返回null。因此,它所做的只是将一些数据读入位桶。这符合计划吗?由于initFromScanner返回null,很明显,依赖WebGraph类对象的任何测试都无法工作。方法hasLink使用一个循环来检查参数j是否是从0到wg[i]的值之一。长度-1。只需将j与0和另一个值进行比较即可检查。-可能你是指其他值吗?getNodeSize方法看起来像一个“getter”。当它读取和设置一个实例变量时,它实际上是一个“getter”和一个“setter”,这将混淆该类的所有用户并引入错误。方法linkWeight(int a,int b)有两个参数,两个参数都没有使用。您写道,它应该返回一些东西(测试尝试匹配其返回值),但是,唉,它是一个无效的方法。
public class I5Exc4aTest {
    WebGraph wg;
    /**
     * This will be invoked before each testcase.
     * Ensure to copy/paste the content of file
     * 'ext/web1.txt' to the Eclipse console.
     * In L6, we will teach you how to automate that!
     * @throws Exception
     */
    @Before
    public void setUp() throws Exception {
        Scanner s = new Scanner(System.in);
        System.out.println("Please copy/paste the content of "
                + "file 'ext/web1.txt' to this console. "
                + "Then, press ENTER.");
        wg = WebGraph.initFromScanner(s);
    }

@Test
public void testGetNodeSize() {
    assertEquals(50, wg.getNodeSize());
}
@Test
public void testHasLink1() {
    assertFalse(wg.hasLink(0, 0));
}
@Test
public void testHasLink2() {
    assertTrue(wg.hasLink(0, 7));
}
@Test
public void testLinkWeight1() {
    assertEquals(1, wg.linkWeight(0, 7));
}
@Test
public void testLinkWeight2() {
    assertEquals(1, wg.linkWeight(0, 34));
}
@Test
public void testLinkWeight3() {
    assertEquals(2, wg.linkWeight(1, 22));
}
@Test
public void testSuccessorsSortedList() {
    List<Integer> a = wg.successorsSortedList(1);
    List<Integer> expected = Arrays.asList(new Integer(14),
            new Integer(22), new Integer(22), new Integer(45));
    assertTrue("Expected 'a' and 'expected' to be equal."+
            "\n 'a' = "+a+
            "\n 'expected' = "+expected,
            expected.equals(a));
}
@Test
public void testSuccessorsSortedSet() {
    Set<Integer> a = wg.successorsSortedSet(1);
    Set<Integer> expected = new java.util.TreeSet<Integer>(
            Arrays.asList(new Integer(14), new Integer(22), new Integer(45)));
    assertTrue("Expected 'a' and 'expected' to be equal."+
            "\n 'a' = "+a+
            "\n 'expected' = "+expected,
            expected.equals(a));
}
@Test
public void testAsMatrix1() {
    int[][] copy= wg.asMatrix();
    assertArrayEquals(new int[]{0, 1, 0, 1, 0, 0}, copy[0]);
    assertArrayEquals(new int[]{1, 0, 1, 0, 0, 1}, copy[1]);
    assertArrayEquals(new int[]{0, 1, 2, 0, 1, 0}, copy[2]);
    assertArrayEquals(new int[]{1, 2, 0, 1, 0, 0}, copy[3]);
    assertArrayEquals(new int[]{1, 1, 0, 0, 0, 1}, copy[4]);
    assertArrayEquals(new int[]{0, 1, 0, 2, 0, 0}, copy[5]);
}
}