Java中的ArrayList,列出了方形数字 - 家庭作业

时间:2013-10-28 02:13:29

标签: java arraylist

我想我已经找到了大部分代码,只有一部分让我感到悲伤。当我使用printList(list)时,它打印出1,2,3,......当它应该打印这些数字的正方形时最多9。如果我打印出createSquaresList(10),则会正确打印。任何帮助表示赞赏: - )。

import java.util.*;

public class Lab8a
{
    public static void main(String args[])
    {
        ArrayList<Double> list = createSquaresList(10);
        printList(list);
        removeElement(list, 4);
        printList(list);
        swapElements(list, 2, 6);
        printList(list);
        double max = getMaxValue(list);
        double ave = getAverage(list);
        System.out.println("Max Value = " + max);
        System.out.println("Average = " + ave);
        int idx1 = linearSearch(list, 4);
        int idx2 = linearSearch(list, 75);
        System.out.println("idx 1 = " + idx1);
        System.out.println("idx 2 = " + idx2);
    }

    public static ArrayList<Double> createSquaresList(int n)
    {
        ArrayList<Double> squares= new ArrayList<>();
        double s = 0.0;
        for (double i = 0.0; i <= n-1; i++)
        {
            s = i*i;
            squares.add(s);
        }
        return squares;
    }

    public static double getMaxValue(ArrayList<Double> list)
    {
        double largest = list.get(0);
        for (int i = 1; i < list.size(); i++)
        {
            if (list.get(i) > largest)
            {
                largest = list.get(i);
            }
        }
        return largest;
    }

    public static double getAverage(ArrayList<Double> list)
    {
          double avg = 0.0;
          double sum = 0.0;
          for (int i =0; i < list.size(); i++)
          {
              sum += list.get(i);
          }
          avg = sum / list.size();
          return avg;
    }

    public static void removeElement(ArrayList<Double> list, double index)
    {
        double temp = 0.0;
        int lastPos = list.size() - 1;
        double last = list.get(lastPos);
        index = temp;
        temp = last;
        last = index;
        list.remove(lastPos);
    }

    public static void swapElements(ArrayList<Double> list, int a, int b)
    {
        int temp = 0;
        a = temp;
        temp = b;
        b = a;

    }

    public static int linearSearch(ArrayList<Double> list, double val)
    {
        int pos = 0;
        boolean found = false;
        while (pos < list.size() && !found)
        {
            if (list.get(pos) == val)
            {
                found = true;
            }
            else
            {
                pos++;
            }
        }
        if (found)
        {
            return pos;
        }
        else
        {
            return -1;
        }
    }

    public static void printList(ArrayList<Double> list)
    {
        for(int i = 0; i < list.size(); i++)
        {
            System.out.print(i);
            if(i < list.size()-1)
            {
                System.out.print(", ");
            }
        }
        System.out.println("");
    }
}

2 个答案:

答案 0 :(得分:11)

更改

System.out.print(i);

System.out.print(list.get(i));

答案 1 :(得分:0)

这是因为你打印的是int而不是列表的内容,

尝试将函数printList的3行更改为:

System.out.print(list.get(i));