如何在指定的索引处将两个数组列表的对象相互传递 [英] How to pass the objects of two arraylists to one another at a specified index

查看:113
本文介绍了如何在指定的索引处将两个数组列表的对象相互传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图按照对象区域的顺序打印出阵列列表.但是,我似乎无法弄清楚如何在索引处将对象的值相互传递. (我必须递归执行). 到目前为止,这是我的代码

So, I am trying to print out my arraylists in order of the objects area. I cannot however seem to figure out how to pass the values of objects to one another at an index. (I must do it recursively). Here is my code thusfar

private static void recursionSort(ArrayList<GeometricObject> data)
        {
            if(data.size() <= 1) return;               // Base case: just 1 elt

            ArrayList<GeometricObject> a = new ArrayList<GeometricObject>(data.size() / 2);
            ArrayList<GeometricObject> b = new ArrayList<GeometricObject>(data.size() - a.size());     // Split array into two
            //   halves, a and b
            for(int i = 0; i < data.size(); i++) 
            {
                if(i < a.size())
                    a.indexOf(i) = data.get(i);
                else             
                    b.get(i - a.size()) = data.get(i);
            }

            recursionSort(a);                              // Recursively sort first
            recursionSort(b);                              //   and second half.

            int ai = 0;                                // Merge halves: ai, bi
            int bi = 0;                                //   track position in
            while(ai + bi < data.size()) {             //   in each half.
                if(bi >= b.size() || (ai < a.size() && a.get(ai).getArea() < b.get(bi).getArea())) {
                    data.get(ai + bi) = a.get(ai); // (copy element of first array over)
                    ai++;
                } else {
                    data.get(ai + bi) = b.get(bi); // (copy element of second array over)
                    bi++;
                }
            }
        }

我的问题在于台词

a.indexOf(i) = data.get(i);
b.get(i - a.size()) = data.get(i);
data.get(ai + bi) = a.get(ai);
data.get(ai + bi) = b.get(bi); 

例如,我不知道如何说a的索引为0等于我列表的(数据)索引为0.如果这些是误入歧途,我会知道该怎么做,所以让我将其用作一个示例,向您展示我仅通过arraylists即可完成的工作

For example I can't figure out how to get say the index of a at 0 to equal my list's (data) index of 0. If these were arrrays, i would know what to do, so let me use that as an example to show you what I'm trying to accomplish just via arraylists

a[i] = data[i]; // First line in block above
data[ai + bi] = b[bi]; // Last line in block above

任何帮助将不胜感激.我已经遍历了我的书中ArrayList类方法列表中找到的每个方法,但都没有达到我想要的效果.谢谢!

Any help would be greatly appreciated. I've went through almsot every method found in my book's list of ArrayList Class methods and none have the desired effect I'm looking for. Thanks!

推荐答案

List接口定义set(int index, E element)(在当前情况下为E = GeometricObject).因此,您遇到问题的四行代码应按以下方式重写:

The List interface defines the set(int index, E element) (E = GeometricObject in the present case). Therefore, the four lines you're having trouble with should be rewritten as follows:

a.set(i, data.get(i));
b.set(i - a.size(), data.get(i));
data.set(ai + bi, a.get(ai));
data.set(ai + bi, b.get(bi));

希望这对您有帮助...

Hope this helps...

杰夫

这篇关于如何在指定的索引处将两个数组列表的对象相互传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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