简体   繁体   English

从Java列表中打印模式

[英]Printing pattern from list in Java

What I have done here is taken the contents of the table below and stored them in a list called tableElems 我在这里所做的是获取下表的内容并将其存储在名为tableElems的列表中

 0  1  2  3  4  5  6  7  8
 9  10 11 12 13 14 15 16 17
 18 19 20 21 22 23 24 25 26
 27 28 29 30 31 32 33 34 35

List<WebElement> tableElems = chrome.findElements(By.tagName("td"));

From this table I want to print in a pattern starting at the 4th element (#3 in the table) and I want to print 3 elements, then skip the next 6, print the next 3 and then skip the next 6, etc. etc. 从该表中,我想以第4个元素(表中的#3)开始的图案进行打印,并且我想打印3个元素,然后跳过下一个6,打印下一个3,然后跳过下一个6,依此类推。 。

So my expected output would be 3 4 5 12 13 14 21 22 23 30 31 32 所以我的预期输出将是3 4 5 12 13 14 21 22 23 30 31 32

My initial attempt ended with 我最初的尝试以

for (int i = 3; i<tableElems.size(); i++) {
            if(i % 3 == 0) { System.out.println(tableElems.get(i).getText()); }
            else if(i % 4 == 0) { System.out.println(tableElems.get(i).getText()); }
            else if(i % 5 == 0) { System.out.println(tableElems.get(i).getText()); }
        }

Obviously this is wrong, because my % 3 will print the 9th element, % 4 will print the 16th element, etc. 显然这是错误的,因为我的% 3将打印第9个元素, % 4将打印第16个元素,依此类推。

I am having trouble wrapping my head around this, so any advice to point me in the right direction is appreciated! 我在解决这个问题时遇到了麻烦,因此,向我指出正确方向的任何建议都值得赞赏!

 int offset = 4;
 int width = 3;

advice: 建议:

for each row in table check offset + width is under the row width, and then start from offset + 0, offset + 1 upto offset + width 对于表中的每一行,请检查偏移量+宽度是否在行宽度之下,然后从偏移量+ 0开始,偏移量+ 1直到偏移量+宽度

and continue this loop for all the row of table 并为表的所有行继续此循环

You mentioned that tableElems is a list, so I assume it's akin to a single-dimension array, rather than what appears to be a multi-dimensional table like you've laid out(I assume for visual purposes). 您提到tableElems是一个列表,所以我认为它类似于一维数组,而不是看起来像您已经布置的多维表(我假设出于视觉目的)。

You're right by starting the for loop at i = 3. The easiest way, IMO, to do this is to just bump the loop ahead several spaces in the array after you've printed three elements. 通过在i = 3处启动for循环,您是对的。最简单的方法,IMO,是在打印了三个元素之后,将循环在数组中的几个空格前向前推。 Here's my solution using C# and an array; 这是我使用C#和数组的解决方案; you should be able to convert it to Java pretty easily. 您应该能够轻松地将其转换为Java。

int counter = 0;
for (int i = 3; i < array.Length; i++)
{
    if (counter < 3)
    {
        Console.WriteLine(array[i]);
        counter++;
    }
    else
    {
        i += 5;  // we are incrementing by one on the next pass, so this can be 5  
        counter = 0;    
    }    
}

try this: 尝试这个:

for (int i = 3; i<tableElems.size(); i++) {
    if (((i / 3) % 3) == 1) { System.out.println(tableElems.get(i).getText()); }
}

Think about this as dividing into groups of nine, and further dividing each group of 9 into groups of 3. on the second group of 3 (ie 3-5, 12-14) it will display. 考虑将其分为9个组,然后将9个组进一步分为3组。在第二个3组(即3-5、12-14)上,它将显示。

If you want to stick to % , then this should work 如果您要坚持% ,那应该可以

int rowSize = 9;
for (int i = 0; i < tableElems.size(); i++) {

    int column = i % rowSize;

    if (column >=3 && column <= 5) {
        System.out.println(tableElems.get(i).getText());
    }
}

Update: Java code 更新:Java代码

for (int i = 3; i<tableElems.size(); i += 9) {
    System.out.println(tableElems.get(i).getText());
    System.out.println(tableElems.get(i+1).getText());
    System.out.println(tableElems.get(i+2).getText());
    System.out.println(tableElems.get(i+3).getText());
}

If you are creating a larger pattern: 如果要创建更大的模式:

int starting = 3;
int nextStartDistance = 9;
int waveCount = 3;

for (int i = starting; i<tableElems.size(); i += nextStartDistance) {
    for (int b = 0; b <= waveCount; b ++) {
        System.out.println(tableElems.get(i).getText());
    }

}

This code is written in JavaScript, but the algorithm considered: 这段代码是用JavaScript编写的,但是算法考虑了:

var list =[
0,1,2,3,4,5,6,7,8,
9,10,11,12,13,14,15,16,17,
18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32,33,34,35];

for (var index = 3; index <= list.length; index +=9 ) {
    var a = list[index];
    var b = list[index+1];
    var c = list[index+2];
    var d = list[index+3];
    console.log(a,b,c,d);
}

Result will be what you expected: 结果将是您期望的:

// 3 4 5 6
// 12 13 14 15
// 21 22 23 24
// 30 31 32 33
// user:~$ 
for (int i = 3; i < tableElems.size(); i += 9) {
    for (int j = i; j < i + 3 && j < tableElems.size(); j++) {
        System.out.println(tableElems.get(j).getText());
    }
}

i think this would be very close to your code: 我认为这将非常接近您的代码:

for (int i = 3; i < tableElems.size(); i++) {
        if (i % 3 == 0) {
            System.out.println(tableElems.get(i).getText());
        } else if ((i % 3) == 1) {
            System.out.println(tableElems.get(i).getText());
        } else if ((i % 3) == 2) {
            System.out.println(tableElems.get(i).getText());
            i = i + 6;
        }
    }

i just adapted your modulo and skip 6 elements every time i hit the last one to print out 我只是调整了您的模数,每当我点击最后一个要打印时,就跳过6个元素

furthermore its quite effective because all unneeded elements are just skipped instead of iterated and checked 此外,它非常有效,因为所有不需要的元素都被跳过,而不是迭代和检查

Assuming you only care about the values out of your table and the html structure is standard html tags as shown on your questions, then you can use css selector to select appropriate values. 假设您只关心表中的值,并且html结构是问题中显示的标准html标签,则可以使用css选择器选择适当的值。

chrome.findElements(By.cssSelector("td:nth-child(4),td:nth-child(5), td:nth-child(6)"))

Please note that css nth-child selector is 1 based. 请注意,css nth-child选择器基于1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM