检查元素是否在List中
import java.util.ArrayList;
【检查元素是否在List中】public class ExistanceForList {
public static void main(String[] args) {
// TODO Auto-generated method stub
// create a new ArrayList of Strings with an initial capacity of 10
ArrayList items = new ArrayList();
items.add("red");
// append an item to the list
items.add(0, "yellow");
// insert "yellow" at index 0// check if a value is in the List
System.out.printf("\"red\" is %sin the list%n",
items.contains("red") ? "": "not ");
// display number of elements in the List
System.out.printf("Size: %s%n", items.size());
items.remove(1);
// remove item at index 1
display(items, "Remove second list element (green):");
// check if a value is in the List
System.out.printf("\"red\" is %sin the list%n",
items.contains("red") ? "": "not ");
// display number of elements in the List
System.out.printf("Size: %s%n", items.size());
} // display the ArrayList's elements on the console
public static void display(ArrayList items, String header)
{
System.out.print(header);
// display header// display each element in items
for (String item : items)
System.out.printf(" %s", item);
System.out.println();
}
}
Console:
"red" is in the list
Size: 2
Remove second list element (green): yellow
"red" is not in the list
Size: 1
推荐阅读
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 你是否也是一道风景()
- C语言解方程的根和判断是否是闰年
- 那一年我是否经历过高考
- 塔罗占卜(近期是否会遇到避不开的劫数(准爆了))
- 我们是否会娱乐至死()
- 韩信(工资是否应该透明)
- 这周你回家么()
- 你也许不知道的Vuejs|你也许不知道的Vuejs - 花式渲染目标元素
- React.js的表单(六)