开发者

basic search through array

开发者 https://www.devze.com 2023-03-01 11:34 出处:网络
Name = Input.next(); for(int K = 0; K < CurrentCount ; K++) { if( ArrayList[K].LastN开发者_运维问答amePlayer == Name)
Name = Input.next();

for(int K = 0; K < CurrentCount ; K++) {
  if( ArrayList[K].LastN开发者_运维问答amePlayer == Name)
    System.out.println(
      "Name " + ArrayList[K].GetName() + ArrayList[K].GetLastName());

I can access all the values in the array but when I set a condition it returns nothing when it should.


You're comparing strings by reference. In other words, you're checking whether the references ArrayList[K].LastNamePlayer and Name are references to the exact same object. I suspect you want:

if (ArrayList[K].LastNamePlayer.equals(Name))

which will check for string equality... whether they're references to equal strings: ones which represent the same sequence of characters.

Note that these variable names are unconventional for Java - normally you'd have name, lastNamePlayer etc, using camel case instead of Pascal case. Similarly the method name would normally be getName() instead of GetName().

Also, it looks like ArrayList is an array, not an ArrayList, so it's a pretty misleading name. It would be more useful if it described what it represented rather than the storage, too.


Instead of

if( ArrayList[K].LastNamePlayer == Name)

write

if( ArrayList[K].LastNamePlayer.equals(Name))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号