英文:
What is The Appropriate Data Structure For Modifying Some Elements of a Collection In Place?
问题
假设我有一组对象,并且对于每个具有特定属性的对象,我想要删除该对象,并插入一个新对象来代替它。举个例子,假设我有一组动物对象:
[狗,狗,狗,coyote,狗,狐狸,狗,猫,雪貂,土拨鼠]
对于每个动物 ->
如果动物是狗、雪貂、土拨鼠,则继续迭代
否则,如果动物是coyote,则替换为狗并继续迭代
否则,如果动物是狐狸,则替换为狗并继续迭代
在保持集合的初始顺序的同时,哪种数据结构最适合实现这样的操作?任何建议都将不胜感激。
英文:
Say I have a collection of objects, and for every object that has a certain property,I would like to remove that object and insert a new object in its place. As an example lets say I have a collection of animal objects:
[Dog, Dog, Dog, Coyote, Dog, Fox, Dog, Cat, Ferret, Groundhog]
for each animal ->
if animal = Dog, Ferret, Groundhog continue iterating
else if animal = Coyote, replace with Dog and continue iterating
else if animal = Fox, replace with Dog and continue iterating
Which data structure would be the best for accomplishing something like this while maintaining the initial order of the collection? Any advice is appreciated.
答案1
得分: 0
以下是翻译好的部分:
一个数组是您可以使用的最轻量级数据结构,如果元素的数量不变,则最合适。唯一的问题是您必须将数组的引用类型声明为全部相等。我建议所有的类都从一个父类,比如 'Animal' 类进行扩展。
在下面的解决方案中,我已经将所有的类都扩展自 Animal:
class Animal{
}
class Dog extends Animal{
}
class Cat extends Animal{
}
class Coyote extends Animal{
}
class Fox extends Animal{
}
class Ferret extends Animal{
}
class Groundhog extends Animal{
}
然后,我使用数组来存储 Animal 实例。
然后,使用 for 循环将遍历每个项目,并将 Fox 和 Coyote 的实例替换为 Dog。
public static void main(String[] args) {
Dog a = new Dog();
Dog b = new Dog();
Dog c = new Dog();
Coyote d = new Coyote();
Dog e = new Dog();
Fox f = new Fox();
Dog g = new Dog();
Cat h = new Cat();
Ferret i = new Ferret();
Groundhog j = new Groundhog();
Animal[] animalArray = new Animal[10];
animalArray[0] = a;
animalArray[1] = b;
animalArray[2] = c;
animalArray[3] = d;
animalArray[4] = e;
animalArray[5] = f;
animalArray[6] = g;
animalArray[7] = h;
animalArray[8] = i;
animalArray[9] = j;
for(int ii = 0; ii<animalArray.length; ii++){
if (animalArray[ii] instanceof Coyote || animalArray[ii] instanceof Fox){
animalArray[ii] = new Dog();
}
}
}
如果您在 public static void main 方法中包含以下代码,您可以运行该方法以获得以下输出。
for(Animal animal: animalArray){
System.out.println(animal.getClass());
}
输出(其中 'Test' 是包名)。
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Cat
class Test.Ferret
class Test.Groundhog
英文:
An array is the most light-weight data structure you can use and most appropriate if the number of elements does not change. The only issue is that you must declare the reference type of the array to all be equal. I would suggest all the classes extend from a parent class, say 'Animal'.
In the solution below, I have extended all the classes from Animal:
class Animal{
}
class Dog extends Animal{
}
class Cat extends Animal{
}
class Coyote extends Animal{
}
class Fox extends Animal{
}
class Ferret extends Animal{
}
class Groundhog extends Animal{
}
I have then used an array to store the Animal instances.
A for-loop will then iterate through each item and replace the instances of Fox and Coyote with Dog.
public static void main(String[] args) {
Dog a = new Dog();
Dog b = new Dog();
Dog c = new Dog();
Coyote d = new Coyote();
Dog e = new Dog();
Fox f = new Fox();
Dog g = new Dog();
Cat h = new Cat();
Ferret i = new Ferret();
Groundhog j = new Groundhog();
Animal[] animalArray = new Animal[10];
animalArray[0] = a;
animalArray[1] = b;
animalArray[2] = c;
animalArray[3] = d;
animalArray[4] = e;
animalArray[5] = f;
animalArray[6] = g;
animalArray[7] = h;
animalArray[8] = i;
animalArray[9] = j;
for(int ii = 0; ii<animalArray.length; ii++){
if (animalArray[ii] instanceof Coyote || animalArray[ii] instanceof Fox){
animalArray[ii] = new Dog();
}
}
}
If you include the below code in the public static void main method, you can run the method to get the following output.
for(Animal animal: animalArray){
System.out.println(animal.getClass());
}
Output (where 'Test' is the package name):
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Dog
class Test.Cat
class Test.Ferret
class Test.Groundhog
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论