我有一个列表,其中包含元素1到10.我尝试从中删除素数2,3,5,7然后使用iterator打印列表的其余部分.但此代码抛出 NoSuchElementException.这是我的代码:
public static void editerate2(Collection<Integer> list3)
{
Iterator<Integer> it=list3.iterator();
while(it.hasNext())
{
if(it.next()==2 || it.next()==3 || it.next() ==5 || it.next()==7 )
{
it.remove();
}
}
System.out.println("List 3:");
System.out.println("After removing prime numbers : " + list3);
}
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?还有什么区别使用"|" 和"||" ???
我试图使用compareToIgnoreCase对字符串数组进行排序.
该字符串包含名称ex:Billy Jean
当我尝试比较它们时,我得到一个空指针异常.我认为这是因为名字和姓氏之间的空格.我如何比较全名?
谢谢
class CompareStudentNames implements Comparator{
//Sorts the students by name ignoring case.
@Override
public int compare(Object o1, Object o2) {
String name1 = ((Student)o1).getName();
String name2 = ((Student)o2).getName();
return (name1.compareToIgnoreCase(name2));
}
Run Code Online (Sandbox Code Playgroud)
}
编辑---在使用比较学生姓名的代码中添加
private static void newInputFileReading(File f) throws FileNotFoundException{
String inputLine = null;
String [] inputSplit = new String[7];
Boolean proceed = false;
Scanner enterReader = new Scanner(System.in);
String name;
while(!proceed){
int stunum = -1;
try {
Scanner inputReader = new Scanner(f);
while(inputReader.hasNextLine()){
studentNM.add(new Student()); …
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码,以查找数字600851475143的最大素数:
public class Three {
public static void main(String[] args) {
int counter = 0, largePrime = 0;
for (int i = 2; i <= 600851475143; i++){
for(int x = 2; x < = i; x++){
if(i %x == 0){
counter++;
}
}
if (counter == 1){
// System.out.println(i);
largePrime=i;
}
counter=0;
}
System.out.println(largePrime);
}
}
Run Code Online (Sandbox Code Playgroud)
Java无法运行,因为它超出了int限制.我也试过long,double和BigInteger,但无济于事.有没有办法让for循环迭代超过2.17b次?
它接受文本字段的值,我试图拥有它,如果任何数字在字段中然后我能够阻止它.
function hasNumbers(input) {
var length = input.length;
var end = 0;
for (var x = 0; x < length; x++) {
var char = input.substring(x, 1);
trueFalse = isNaN(char);
if (! trueFalse) {
end++;
}
}
if (end > 0) {
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud) java ×3
arraylist ×1
collections ×1
comparator ×1
iterator ×1
javascript ×1
regex ×1
sorting ×1
validation ×1