If语句不是';t与两个人一起工作<;长期>;列表[Java]
以下是供参考的完整方法:If语句不是';t与两个人一起工作<;长期>;列表[Java],java,if-statement,arraylist,Java,If Statement,Arraylist,以下是供参考的完整方法: temp is:900876512 temp is:765867999 temp is:465979798 temp is:760098908 temp is:529086890 temp is:765867999 temp is:529086890 temp is:800003243 temp is:200900210 temp is:200900210 temp is:542087665 temp is:900876512 temp is:900876512 tem
temp is:900876512
temp is:765867999
temp is:465979798
temp is:760098908
temp is:529086890
temp is:765867999
temp is:529086890
temp is:800003243
temp is:200900210
temp is:200900210
temp is:542087665
temp is:900876512
temp is:900876512
temp is:900876512
temp is:900876512
publicstaticvoidcheckcontents(BufferedReader infiestreamname、File aFile、Scanner s)抛出DuplicateSerialNumberException
{
System.out.println();
List ls=新的ArrayList();
List ls2=新的ArrayList();
long SerialNum=0;
int计数器=0;
int size=0;
字符串缓冲区;
而(s.hasNextLine())
{
ls.add(s.nextLong());
//System.out.println();
StringBuffer=s.nextLine();
ls2.add(StringBuffer);
size=ls.size();
//System.out.println(ls.size());
SerialNum=ls.get(size-1);
//System.out.println(ls.get(size-1));
System.out.println(“Serial”是:“+SerialNum”);
//System.out.println(SerialNum+“:”+StringBuffer);
计数器++;
}
长温;
对于(int i=0;i
一个不错的
在列表中,您不存储float
(原语),而是存储float
对象。自动装箱使您对自己透明
而且==
比较器不处理对象(它告诉您两个对象是否相同,但如果有两个对象持有相同的值,则返回false
)
你可以用
public static void CheckContents(BufferedReader inFileStreamName, File aFile, Scanner s ) throws DuplicateSerialNumberException
{
System.out.println();
List<Long> ls = new ArrayList<Long>();
List<String> ls2 = new ArrayList<String>();
long SerialNum = 0;
int counter = 0;
int size = 0;
String StringBuffer;
while (s.hasNextLine())
{
ls.add(s.nextLong());
//System.out.println();
StringBuffer = s.nextLine();
ls2.add(StringBuffer);
size = ls.size();
//System.out.println(ls.size());
SerialNum = ls.get(size-1);
//System.out.println(ls.get(size-1));
System.out.println("Serial # is: " + SerialNum);
//System.out.println(SerialNum + ": " + StringBuffer);
counter++;
}
long temp;
for ( int i = 0; i < size; i++ )
{
temp = ls.get(i);
System.out.println("temp is:" + temp);
for ( int j = 0; j < size; j++)
{
if ( i == j )
{
}
else if ( ls.get(i) == ls.get(j) )
{
// If Duplicate, do stuff... Dunno what yet.
System.out.println(ls.get(i)+" is the same as: " + ls.get(j) );
//System.out.println("Duplicate");
}
}
}
}
或者(因为它是列表
)
甚至(多亏了自动装箱)
请试一试
if ( ls.get(j).longValue() == ls.get(i))
相反由于某种原因,当我将ls.get(j)=temp,然后用temp计算ls.get(I)时,这会正常工作,但不会用ls.get(j)计算ls.get(I)。。。代码现在可以工作了,但如果知道为什么会这样,那就太好了……它被称为自动装箱。在列表中,您只能存储对象,但当您将其指定给原语时,转换是自动的(当您将原语添加到列表中时,转换为对象也是自动的)。哇,我应该意识到它是作为对象而不是原语存储的,不确定哪一个是正确的,因为这两个答案都有帮助:Dshuan的答案更完整,所以你接受他的答案做得很好
if ( ls.get(j).equals(ls.get(i))
if ( ls.get(j).longValue() == ls.get(i).longValue())
if ( ls.get(j).longValue() == ls.get(i))
else if ( ls.get(i) != null && ls.get(i).equals(ls.get(j)) )