Monster_Orange Monster_Orange
关注数: 4 粉丝数: 11 发帖数: 1,011 关注贴吧数: 28
求教各位大神,请教一个Java ArrayList的问题 …… 同学给我这个菜鸟除了一道题: http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Fcodecheck.it%2Fcodecheck%2Ffiles%3Frepo%3Dsjsucs46b%26problem%3Dhw1a%26level%3D2&urlrefer=15b4f773bd087f7b338f17c144f31489 Use the following file: ArrayOpsTester.java import java.util.*; public class ArrayOpsTester { public static ArrayList<Integer> listOfIntegers(int... values) { ArrayList<Integer> result = new ArrayList<>(); for (int v : values) result.add(v); return result; } public static void main(String[] args) { ArrayList<Integer> input = listOfIntegers(2, 14, 9, 15, 9, 7, 3, 17, 14); int[] result = ArrayOps.copyArrayList(input); System.out.println(input + " -> " + Arrays.toString(result)); System.out.println("Expected: [2, 14, 9, 15, 9, 7, 3, 17, 14] -> [2, 14, 9, 15, 7, 3, 17]"); input = listOfIntegers(61, 61); result = ArrayOps.copyArrayList(input); System.out.println(input + " -> " + Arrays.toString(result)); System.out.println("Expected: [61, 61] -> [61]"); input = listOfIntegers(143); result = ArrayOps.copyArrayList(input); System.out.println(input + " -> " + Arrays.toString(result)); System.out.println("Expected: [143] -> [143]"); input = new ArrayList<>(); result = ArrayOps.copyArrayList(input); System.out.println(input + " -> " + Arrays.toString(result)); System.out.println("Expected: [] -> []"); } } Complete the following file: ArrayOps.java import java.util.ArrayList; public class ArrayOps { /** This method goes through the given array of integers, yielding a new ArrayList from the array that contains the elements of the original array, with duplicates removed. @param theArray, an array of integer @return the new ArrayList */ public static ArrayList<Integer> copyArray(int[] anArray) { // Complete this for the draft return null; } /** This method goes through the given array list of integers, yielding a new array from the array list that contains the elements of the original array list, with duplicates removed. @param theArrayList, an array list of integer @return the new array */ public static int[] copyArrayList(ArrayList<Integer> anArrayList) { // Complete this for the final version return null; } } 尝试地写了一下: public class ArrayOps { public static ArrayList<Integer> copyArray(int[] anArray) { // Complete this for the draft ArrayList<Integer> outputArrayList=new ArrayList<Integer>(); if (anArray.length>1) { for (int i=0;i<anArray.length-1;i++) { outputArrayList.add(anArray[i]); } } return outputArrayList; } public static int[] copyArrayList(ArrayList<Integer> anArrayList) { // Complete this for the final version int[] outputArray={0}; if (anArrayList.size()==1) { outputArray[0]=anArrayList.get(0); } else { for(int i=0;i<anArrayList.size()-1;i++) { outputArray=new int[anArrayList.size()-1]; outputArray[i]=anArrayList.get(i); } } return outputArray; } } 但是出来的结果不对啊: [2, 14, 9, 15, 9, 7, 3, 17, 14] -> [0, 0, 0, 0, 0, 0, 0, 17] Expected: [2, 14, 9, 15, 9, 7, 3, 17, 14] -> [2, 14, 9, 15, 7, 3, 17] [61, 60] -> [61] Expected: [61, 61] -> [61] [143] -> [143] Expected: [143] -> [143] [] -> [0] Expected: [] -> [] 貌似长的都不行 …… 到底怎么回事啊 ……
1 下一页