Constructor for array to vector
843785Feb 27 2009 — edited Feb 27 2009I want to take an array (array1) and eventually two ArrayLists (list1 and list2) and have them changed into a class called RealVector which should leave them as vectors. I'm trying right now with just the array but it doesnt recognize the type RealVector as a vector. When I try to do v1.size(), it says RealVector doesnt have a size() method.
I've put the main program and the class RealVector program below. I'm using a constructor so I can take the ArrayLists and do them later. What am I doing wrong?
Thanks
*********************************HaveVector.java
import java.util.*;
public class HaveVector {
private static final int vectorSize = 5;
public static void main(String[] args) {
double[] array1 = new double[vectorSize];
ArrayList<Double> list1 = new ArrayList<Double>();
ArrayList<Double> list2 = new ArrayList<Double>();
// Initialize array1 and list1 with the reciprocals
// the first vectorSize positive integers.
for (int index = 0; index < array1.length; index++) {
double reciprocal = 1.0/(index+1);
array1[array1.length-index-1] = reciprocal;
list1.add(reciprocal);
}
// Initialize list2 with the squares of the
// the first vectorSize-1 positive integers.
for (int index = 0; index < vectorSize-1; index++) {
list2.add(new Double(index*index));
}
// Create vectors 1, 2, and 3 from array1, list1, and
// list2 respectively.
RealVector v1 = new RealVector(array1);
System.out.println("Vectors being tested");
System.out.println("--------------------------------------------");
System.out.println("v1: " + v1 + v1.size());
}
public static long factorial(long n) {
if (n == 0) {
return 1;
}
else {
return n * factorial(n-1);
}
}
}
********************** RealVector.java
import java.util.*;
/* this is declaring the class */
public class RealVector {
Vector v1;
/* This is a constructor that takes in arr1 from Test1RealVector */
public RealVector(double[] arr1) {
/* This creates an element in v1 for each element in the array */
v1 = new Vector();
for (int index = 0;index < arr1.length; index++) {
v1.add(arr1[index]);
}
}
}