如何获取列表的下一个树元素(How to get the next tree elements of a list)

编程入门 行业动态 更新时间:2024-10-19 08:54:17
如何获取列表的下一个树元素(How to get the next tree elements of a list)

这个问题更多的是关于最佳实践,而不是从列表中获取元素。

我有一个ArrayList,我通过使用一个简单的for循环来实现它。 如果显示某个关键字,我需要将下一个树元素与特定模式进行比较。

private static boolean areVectorArgumentsValid(ArrayList<String> fileContent) { for (int i=0; i<fileContent.size(); i++) { if (fileContent.get(i).equals(NORMAL) || fileContent.get(i).equals(VERTEX)) { // get the next three elements of "fileContent" and see if they match a certain pattern } } return true; }

我的第一种方法是在实际的outler循环中使用另一个for循环,然后将i递增3:

for (int j=i+1; j<=i+3; j++) { if (!fileContent.get(j).matches(PATTERN)) { return false; } } i+=3;

如你所见,让方法做我想做的事并不难,但是......我不确定是否有一种方法可以让你称之为更优雅

This question is more about best practice than about fetching elements from a list.

I have an ArrayList which I am itaration though by using a simple for-loop. In case a certain keyword shows up, I need to compare the next tree elements to a certain pattern.

private static boolean areVectorArgumentsValid(ArrayList<String> fileContent) { for (int i=0; i<fileContent.size(); i++) { if (fileContent.get(i).equals(NORMAL) || fileContent.get(i).equals(VERTEX)) { // get the next three elements of "fileContent" and see if they match a certain pattern } } return true; }

My first approach would be to use another for-loop within the actual outler loop and then increment i by 3:

for (int j=i+1; j<=i+3; j++) { if (!fileContent.get(j).matches(PATTERN)) { return false; } } i+=3;

As you see, it is not hard to make the method do what I want it to do, but... I am not sure if there might be a way which you'd call more elegant.

最满意答案

这个问题更多的是关于最佳实践,而不是从列表中获取元素。

在详细介绍之前几点评论..

NORMAL.equals(fileContent.get(i))而不是fileContent.get(i).equals(NORMAL)将避免NullPointerException 在迭代下三个元素之前,您应首先检查您的List是否具有下三个元素,以避免ArrayIndexOutOfBoundException

现在如果它只是检查接下来的三个元素,并且只有当三个元素中的任何一个与模式不匹配时才返回false,而不是像下面那样,

if (fileContent.size() < i + 3 && (!fileContent.get(i+1).matches(PATTERN) || !fileContent.get(i+2).matches(PATTERN) || !fileContent.get(i+3).matches(PATTERN))) { return false; }

这种方法的问题是它不会检查您的列表是否没有下三个元素。

至于你的方法,通过允许检查下一个可用的元素你可以添加一个条件来检查你的列表是否在循环中调用get方法之前是否有循环中的下一个元素。你的方法迭代下三个元素似乎很好但是在改进之后需要。

for (int j=i+1; j<=i+3; j++){ if (fileContent.size() < j && !fileContent.get(j).matches(PATTERN)){ return false; } else { break; } }

This question is more about best practice than about fetching elements from a list.

Before going into details few remarks..

NORMAL.equals(fileContent.get(i)) instead of fileContent.get(i).equals(NORMAL) will avoid NullPointerException Before iterating for next three element you should first check whether your List has next three element or not to avoid ArrayIndexOutOfBoundException

Now If it's about checking for only next three elements and only return false if any of the three element does not match the pattern than you can have something like following,

if (fileContent.size() < i + 3 && (!fileContent.get(i+1).matches(PATTERN) || !fileContent.get(i+2).matches(PATTERN) || !fileContent.get(i+3).matches(PATTERN))) { return false; }

Problem in this approach is it will not check if your list does not have next three elements.

As for your approach by allowing check of next available elements you can just add one condition to check whether your list has next element or not in loop before calling get method on the list.Your approach to iterate over next three element seems fine but following improvement is needed.

for (int j=i+1; j<=i+3; j++){ if (fileContent.size() < j && !fileContent.get(j).matches(PATTERN)){ return false; } else { break; } }

更多推荐

本文发布于:2023-04-28 05:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   列表   tree   elements   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!