有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在ArrayList中交换两个对象

我试图在一个ArrayList内交换两个对象。为了实现这一点,我创建了一个新的列表,在其中交换对象,然后用交换的列表完全覆盖旧的列表。但是,我在将旧列表中的对象添加到新列表时遇到问题

该程序从文本文件获取输入,将数据读入对象(圆和矩形,它们是GeometricObject的扩展),然后将这些对象添加到名为objectListArrayList

以下是代码:

public static <E extends Comparable<E>> void swapCells
(ArrayList<E> objectList, int left, int right) {

    /* The user may enter the two indices, "left,"
     * and, "right," in any order which they desire.
     * Because of this it will be necessary to determine
     * which is larger or "right" index, and which is
     * the smaller or "left" index
     */

    int temp;
    ArrayList<GeometricObject> swappedList = new ArrayList<GeometricObject>();

    if (left > right) {

        // Exchanges left and right
        temp = left;
        left = right;
        right = temp;

    }

    for (int i = 0; i < objectList.size(); i++) {

        if (i == left) {
            swappedList.add(objectList.get(right));
            System.out.println( swappedList.get(i).getArea());
        } else {
            swappedList.add((E) objectList.get(i));

        }

    }

} // End of swapCells

我发现以下语法错误,不知道该怎么办

The method add(GeometricObject) in the type ArrayList<GeometricObject> is not applicable for the arguments (E)

错误具体出现在swappedList.add(objectList.get(right));wappedList.add((E) objectList.get(i));


共 (3) 个答案

  1. # 1 楼答案

    您正在尝试将E对象添加到GeometricObject列表中。这就是为什么你会出错。你的列表swappedList应该是ArrayList<E>类型,或者更好:List<E>

    另一方面,您可以将函数的类型修改为:

    public static void swapCells(ArrayList<GeometricObject> objectList, int left, int right)
    

    哦,用你建立的清单做点什么。你当前的代码会丢弃它

  2. # 2 楼答案

    我不相信这正是你想要的答案,但它可能会有所帮助

    如果你用GeomtricObject进行类型转换,你会得到一个有效的代码,然而,如果你只是想把一个泛型强制转换成一个几何对象,那么这就违背了使用泛型的目的

    如果要将左侧对象切换到右侧位置,还需要添加else

    您可能还需要打印交换列表,以确认操作已完成

            for (int i = 0; i < objectList.size(); i++) 
                {
                    if (i == left) {
                        swappedList.add((GeometricObject) objectList.get(right));
                    }else if (i == right)
                        swappedList.add((GeometricObject) objectList.get(left));
                    else {
                        swappedList.add((GeometricObject) objectList.get(i));
                }
            }
    

    编辑2: 以下内容将帮助您在泛型中寻找操作

    您需要创建一个temp并将其转换为E。您还需要在正确的参数和/或形式/符号中使用以下代码

            E temp
            List.set(____ , _____)
            List.get(____ )
    

    如果你仍然有这个交换功能的问题,看看一个不是通用的

    编辑3: 你很可能和我有同样的问题,你还需要对泛型进行排序。您可以使用下面的selectionSort方法来帮助您完成作业。您需要更改该方法,使其适用于ArrayList,而不是数组。这意味着您需要利用编辑2中的建议来修改下面的代码。您可能还需要使用compareTo方法

            private static void selectionSort(int[] list, int low, int high) { 
            if (low < high) { 
              int posMax = high; 
              int theMax = list[high]; 
              for (int i = 0; i < high; i++) { 
                if (list[i] > theMax) { 
                  theMax = list[i]; 
                  posMax = i; 
                }// if 
              }// for 
            list[posMax] = list[high]; 
            list[high] = theMax; 
            selectionSort(list, low, high - 1); 
            }// if 
        }
    
  3. # 3 楼答案

    首先,我要感谢所有为回答这个问题做出贡献的人。上周某个时候,我在办公时间咨询了老师。我的老师让我在写代码之前先在脑海中画出问题所在,然后在纸上和铅笔上画出要使用的过程的物理图。最后,在编写代码之后,以下是解决方案:

    public static <E extends Comparable<E>> void swapCells
    (ArrayList<E> objectList, int left, int right) {
    
        /*
         * Create a temporary generic object so that the left and
         * right objects can be swapped without losing any data.
         */
        E temp = objectList.get(left);
    
        // Place the right object into the left position
        objectList.set(left, objectList.get(right));
    
        /*
         * Place the temporary (left) object into the right
         * position.
         */
        objectList.set(right, temp);
    
    } // End of swapCells
    

    实际上甚至不需要创建第二个数组列表,只要使用一个临时对象E即可