简体   繁体   中英

adding an array into an ArrayList

I know that I am doing something silly here but I am trying to write my first piece of code using Java Arraylists and I have got myself confused.

Basically I have created an Arraylist called allPays. This is going to be an array list of employee pay for each month of the year. So if I have 10 employees then the array list will contain 10 arrays of 12 months pay.

I have then created my first array of 12 months pay.

I then try and call a method to add the array to the Arraylist.

Put it is not compiling …… can anyone guide me on where I am going wrong. Sorry that this appears basic but I am finding it a challenging concept.

    package christmas;
    import java.util.ArrayList;

    public class Pays {

        public static void main(String[] args) {


        ArrayList allPays = new ArrayList<Double>();

        double[] employeePay = {10.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8,0, 9.0, 10.0, 11.0, 12.0};

        addtothearray(employeePay);

         }


        public static void addtothearray(double[] Pay) {


        allPays.add(Pay);

        }
     }

Adding to an existing List * if your List is type of Double

List<Double> allPays = new ArrayList<>();
double[] employeePay = {10.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8,0, 9.0, 10.0, 11.0, 12.0};
DoubleStream.of(employeePay).forEach(allPays::add);

Adding to an existing List * if your List is type of double[]

List<double[]> allPays = new ArrayList<>();
double[] employeePay = {10.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8,0, 9.0, 10.0, 11.0, 12.0};
allPays.add(employeePay);

You can do it as follows:

import java.util.ArrayList;
import java.util.Arrays;

public class Pays {

    public static void main(String[] args) {
        ArrayList<double[]> allPays = new ArrayList<double[]>();
        double[] employeePay = { 10.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8, 0, 9.0, 10.0, 11.0, 12.0 };
        addtothearray(allPays, employeePay);

        // Test
        System.out.println(Arrays.toString((double[]) allPays.get(0)));
    }

    public static void addtothearray(ArrayList<double[]> allPays, double[] pay) {
        allPays.add(pay);
    }
}

Output:

[10.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 0.0, 9.0, 10.0, 11.0, 12.0]

It does not compile because the variable in the method addtothearray is a local variable in the main method. Passing it as an argument or making it a global variable will solve that compilation problem.

public static void main(String[] args) {
    ArrayList allPays = new ArrayList<Double>();
    double[] employeePay = {10.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8,0, 9.0, 10.0, 11.0, 12.0};
    addtothearray(allPays, employeePay);
}

public static void addtothearray(ArrayList allPays, double[] Pay) {
    allPays.add(Pay);
}

java.util.Collections.addAll(list, array)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM