gdhc21
gdhc21

Reputation: 107

ArrayList in Java that lists square numbers - Homework

I think I have most of the code figured out, there is just one part that is giving me grief. When I use printList(list) it prints out 1, 2, 3,... up to 9 when it is supposed to be printing the squares of these numbers. If I print out the createSquaresList(10) it is correctly printed. Any help is appreciated :-).

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("");
    }
}

Upvotes: 2

Views: 3194

Answers (2)

juanho
juanho

Reputation: 37

it is because you are print the int not the contents of the list,

try changing the 3 line of the function printList to:

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

Upvotes: 0

musical_coder
musical_coder

Reputation: 3896

Change

System.out.print(i);

to

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

Upvotes: 11

Related Questions