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

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

问题描述

我有几个问题其实。

我有一个类的与以下实例字段:

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="";

我也有一个类归档可实例化的并把狗对象插入到一个ArrayList

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

我试图写一个方法归档这需要一个整数作为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?

推荐答案

A ,而适用于后除权pression或块

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

你不必块,让你同时与前pression 狗= al.get(我)结束

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.

有没有理由新的一个狗,因为你永远使用你new'd了狗;你立刻从阵列到你的狗参考分配狗。

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.

如果你需要得到一个键的值,你应该使用地图,而不是一个数组。

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评论:

与问候不必做狗的新实例的一个进一步的问题。如果我只是从数组列表取出对象的副本,我怎么能然后取出从数组列表,而无需一个对象,我把它?我刚才注意到,以及,我没有括号的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.

其结果是,狗狗; 狗狗= NULL 为您提供了一个指向没有对象的引用。 新狗()创建的可指向的对象。

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.

此之后以狗= al.get(I)意味着参考现在指向由 al.get返回的狗引用(我)。明白了,在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).

指针/参考/狗年你newed了地址现在失去了,因为没有code指的是它,因为指涉与您从 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.

其结果是,你需要创建一个可以参考的狗的变量;你不需要创建狗新

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.

(事实上你并不需要创建一个参考,因为你真正应该做的是什么返回从它得到一个返回地图()函数,如果地图不参数化的狗,像这样: 地图&LT;狗&GT; ,那么你就需要投从获得回报,但你不会需要一个参考:收益率(狗)地图获得(ID); 如果映射为参数,返回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天全站免登陆