Java方法:在给定已知属性值的数组列表中查找对象 [英] Java method: Finding object in array list given a known attribute value

查看:47
本文介绍了Java方法:在给定已知属性值的数组列表中查找对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我有几个问题.

我有一个 Dog 类,其中包含以下实例字段:

I have a class Dog with the following instance fields:

private int id;
private int id_mother;
private int id_father;
private String name="";
private String owner="";
private String bDate="";

我还有一个 Archive 类,它可以实例化 Dog 并将 Dog 对象放入一个 ArrayList 中.

I also have a class Archive which can instantiate Dog and put Dog objects into an ArrayList.

我正在尝试在 Archive 中编写一个方法,该方法将整数作为 ID 并查看 ArrayList,并返回包含该 ID 的对象.

I am trying to write a method in Archive which takes an integer as ID and looks through the ArrayList, and returns the object containing that ID.

private Dog getDog(int id){
    Dog dog = new Dog();
    int length=getSize();
    int i=0;

    dog=al.get(i);
    i++;

    while(dog.getId()!=id && i<length)
        dog=al.get(i);
        i++;

    if(dog.getId()!=id)
        dog=null;
    return dog;
}//end getDog

这种方法有两个问题(我使用的其他方法有效).首先它不起作用,我不明白为什么.我正在循环遍历(可能)arraylist 中的所有对象,然后在循环完成后,检查循环是否完成是因为它用完了要搜索的对象,或者因为它找到了具有给定 ID 的对象.其次,这似乎是一个非常耗时的过程.有什么办法可以加快速度吗?

There are two problems with this method (the other methods I use work). First of all it's not working, and I can't see why. I'm while-looping through (potentially) all the objects in the arraylist, for then after the loop is finished, checking whether the loop finished because it ran out of objects to search through, or because it found an object with the given ID. Secondly, that seems like an immensely time-consuming process. Is there some way to speed this up?

推荐答案

while 适用于 while 之后的表达式或块.

A while applies to the expression or block after the while.

您没有块,所以您的 while 以表达式 dog=al.get(i);

You dont have a block, so your while ends with the expression dog=al.get(i);

while(dog.getId()!=id && i<length)
                dog=al.get(i);

之后的一切只发生一次.

Everything after that happens only once.

没有理由重新养狗,因为您从未使用过新养的狗;您立即将数组中的 Dog 分配给您的 dog 引用.

There's no reason to new up a Dog, as you're never using the dog you new'd up; you immediately assign a Dog from the array to your dog reference.

如果你需要获取一个键的值,你应该使用 Map 而不是 Array.

And if you need to get a value for a key, you should use a Map, not an Array.

这是donwmodded为什么??

this was donwmodded why??

来自 OP 的评论:

关于不必创建新的 Dog 实例的另一个问题.如果我只是从数组列表中取出对象的副本,那么如何在没有放置对象的情况下从数组列表中取出它?我也注意到我没有将 while 循环括起来.

One further question with regards to not having to make a new instance of a Dog. If I am just taking out copies of the objects from the array list, how can I then take it out from the array list without having an object in which I put it? I just noticed as well that I didn't bracket the while-loop.

Java 引用和它引用的对象是不同的东西.它们非常像 C++ 引用和对象,尽管 Java 引用可以像 C++ 指针一样重新指向.

A Java reference and the object it refers to are different things. They're very much like a C++ reference and object, though a Java reference can be re-pointed like a C++ pointer.

结果是 Dog dog;Dog dog = null 给你一个不指向任何对象的引用.new Dog() 创建一个可以指向的对象.

The upshot is that Dog dog; or Dog dog = null gives you a reference that points to no object. new Dog() creates an object that can be pointed to.

在后面加上 dog = al.get(i) 意味着引用现在指向 al.get(i) 返回的狗引用.理解,在 Java 中,永远不会返回对象,只返回对对象的引用(即对象在内存中的地址).

Following that with a dog = al.get(i) means that the reference now points to the dog reference returned by al.get(i). Understand, in Java, objects are never returned, only references to objects (which are addresses of the object in memory).

您新建的 Dog 的指针/引用/地址现在丢失了,因为没有代码引用它,因为引用对象被替换为您从 al.get() 获得的引用对象.最终 Java 垃圾收集器将销毁该对象;在 C++ 中,你会泄露"内存.

The pointer/reference/address of the Dog you newed up is now lost, as no code refers to it, as the referent was replaced with the referent you got from al.get(). Eventually the Java garbage collector will destroy that object; in C++ you'd have "leaked" the memory.

结果是您确实需要创建一个可以引用 Dog 的变量;你不需要用 new 创建一个 Dog.

The upshot is that you do need to create a variable that can refer to a Dog; you don't need to create a Dog with new.

(实际上,您不需要创建引用,因为您真正应该做的是返回 Map 从其 get() 函数返回的内容.如果 Map 未在 Dog 上参数化,如下所示:Map<Dog>,那么你需要从 get 中转换返回值,但你不需要引用:return (Dog) map.get(id); 或者如果 Map 被参数化,return map.get(id).那一行就是你的整个函数,在大多数情况下它比迭代数组更快.)

(In truth you don't need to create a reference, as what you really ought to be doing is returning what a Map returns from its get() function. If the Map isn't parametrized on Dog, like this: Map<Dog>, then you'll need to cast the return from get, but you won't need a reference: return (Dog) map.get(id); or if the Map is parameterized, return map.get(id). And that one line is your whole function, and it'll be faster than iterating an array for most cases.)

这篇关于Java方法:在给定已知属性值的数组列表中查找对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆