-1

I have been trying to add and 128 ArrayList of double[][] to another array list. This is how the code looks like:

double[] localData = new double[nSamplesTaken.getValue()];
double[][] data = new double[200][200];
int d = 0;
for (int sampleIdx = 0; sampleIdx < nSamplesTaken.getValue(); ++sampleIdx) {

        ArrayList arr2 = new ArrayList();
        for (int i = 3; i < 17; i++) {
            Edk.INSTANCE.EE_DataGet(hData, i, localData, nSamplesTaken.getValue());
            data[d][i] = localData[sampleIdx];
            arr2.add(data[d][i]);
        }
    System.out.println(arr2);
    }

here, nSamplesTaken.getValue() is always 4,

Edk.INSTANCE.EE_DataGet(hData, i, localData, nSamplesTaken.getValue()); 
data[d][i] = localData[sampleIdx];

gives an array of number (different all the time) from an EEG device some thing like this

3999.48717948718
4388.205128205129
4441.025641025642
3659.48717948718
5070.769230769231
3971.2820512820517
4548.717948717949
4032.307692307693
4684.102564102564
4250.256410256411
4643.589743589744
4168.205128205129
4512.307692307693
4025.6410256410263

so I tried using the ArrayList arr2 = new ArrayList(); and added this data to this arr2 by doing arr2.add(data[d][i]); which gave me

[3998.9743589743593, 4388.717948717949, 4440.512820512821, 3658.461538461539, 5072.307692307693, 3970.7692307692314, 4546.153846153847, 4032.307692307693, 4685.128205128206, 4250.256410256411, 4644.615384615385, 4168.205128205129, 4511.282051282052, 4026.1538461538466]

Now what I wanted in the end was to add 128 of these this list in another array list, but the catch is each array would be of different number which is generated by the Emotiv device. something like this

[[3998.9743589743593, 4388.717948717949, 4440.512820512821, 3658.461538461539, 5072.307692307693, 3970.7692307692314, 4546.153846153847, 4032.307692307693, 4685.128205128206, 4250.256410256411, 4644.615384615385, 4168.205128205129, 4511.282051282052, 4026.1538461538466],
[3999.48717948718, 4388.205128205129, 4441.025641025642, 3659.48717948718, 5070.769230769231, 3971.2820512820517, 4548.717948717949, 4032.307692307693, 4684.102564102564, 4250.256410256411, 4643.589743589744, 4168.205128205129, 4512.307692307693, 4025.6410256410263],
...]

can anyone tell me how to do it?

update:

How is my question related to Double precision?

Akshay
  • 2,622
  • 1
  • 38
  • 71

2 Answers2

1

If you only need to iterate the lists then you could always just join them up without having to copy them.

public class JoinedArray<T> implements Iterable<T> {

    final List<T[]> joined;

    @SafeVarargs
    public JoinedArray(T[]... arrays) {
        joined = Arrays.<T[]>asList(arrays);
    }

    @Override
    public Iterator<T> iterator() {
        return new JoinedIterator();
    }

    private class JoinedIterator implements Iterator<T> {

        // The iterator acrioss the arrays.
        Iterator<T[]> i;
        // The array I am working on.
        T[] a;
        // Where we are in it.
        int ai;
        // The next T to return.
        T next = null;

        private JoinedIterator() {
            i = joined.iterator();
            a = i.hasNext() ? i.next() : null;
            ai = 0;
        }

        @Override
        public boolean hasNext() {
            if (next == null) {
                // a goes to null at the end of i.
                if (a != null) {
                    // End of a?
                    if (ai >= a.length) {
                        // Yes! Next i.
                        if (i.hasNext()) {
                            a = i.next();
                        } else {
                            // Finished.
                            a = null;
                        }
                        ai = 0;
                    }
                    if (a != null) {
                        next = a[ai++];
                    }
                }
            }
            return next != null;
        }

        @Override
        public T next() {
            T n = null;
            if (hasNext()) {
                // Give it to them.
                n = next;
                next = null;
            } else {
                // Not there!!
                throw new NoSuchElementException();
            }
            return n;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported.");
        }
    }

    public static void main(String[] args) {
        String[] one = new String[]{
            "One"
        };
        JoinedArray<String> a = new JoinedArray<>(
                one,
                new String[]{
                    "Two",
                    "Three",
                    "Four",
                    "Five"
                },
                new String[]{
                    "Six",
                    "Seven",
                    "Eight",
                    "Nine"
                });
        for (String s : a) {
            System.out.println(s);
        }
        one[0] = "***";
        for (String s : a) {
            System.out.println(s);
        }

        String[] four = new String[4];
        int copied = a.copyTo(four, 3, 4);
        System.out.println("Copied " + copied + " = " + Arrays.toString(four));

    }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

Probably something like this?

List<List<Double>> myList = new ArrayList();

for (int sampleIdx = 0; sampleIdx < nSamplesTaken.getValue(); ++sampleIdx) {
    List<Double> mySubList = new ArrayList();

    //add values to mySubList

    myList.add(mySubList);
}
Halko Karr-Sajtarevic
  • 2,248
  • 1
  • 16
  • 14
  • 1
    You're creating `ArrayList`s of a raw type. Don't forget to parameterize the constructor call. – Parker Hoyes Aug 27 '15 at 08:38
  • I don't see why this should be required?! It's only checked at compile-time, so it wouldn't make a difference if you add the parameters a second time... - correct me if I'm wrong. – Halko Karr-Sajtarevic Aug 27 '15 at 08:40
  • You could name your variables `flibbergeewinkle` if you wanted to, it doesn't matter. But it's important to make your code clear and readable. See http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – Parker Hoyes Aug 27 '15 at 08:43
  • If you're keen on not taking up too much space, you can use the diamond and simply put `<>` after `ArrayList` but before the parenthesis. The compiler will assume what types it should parameterize, since in this case it's quite clear. – Parker Hoyes Aug 27 '15 at 08:44
  • It's not a raw-type. I've specified the parameters on the left side, so the compiler knows which type it is. If you take a look at the generated class-file, it doesn't matter which type of constructor you use. `new ArrayList();`, `new ArrayList<>()` and `new ArrayList()` all have the same bytecode. – Halko Karr-Sajtarevic Aug 27 '15 at 08:49
  • 1
    I haven't peeked at the bytecode myself, but I'd assume that's because of Java's type-erasure. The information is not retained after compilation. Generics in Java purely serves as extra compile-time checking. In this case it doesn't matter, because the reference type is still parameterized, but I think most people would consider using raw types like that bad convention - because your constructed `ArrayList` is indeed a raw type. I suppose it's all preference. – Parker Hoyes Aug 27 '15 at 08:55
  • Yea that's true, but that's the reason it doesn't matter if you add your type-arguments to the constructor too. You can't add any other type than the specified one to the collection. I think the Java-Compiler got smarter, so it doesn't mind :) – Halko Karr-Sajtarevic Aug 27 '15 at 08:59
  • @HalkoSajtarevic I have tried that but i get get multiples of the same number – Akshay Aug 27 '15 at 22:55