Java Vectors
Implementing Vector Ops in Java
import java.util.ArrayList;
public class Vectors {
public Vectors() {
}
ArrayList<Integer> vector1 = new ArrayList<Integer>();
ArrayList<Integer> vector2 = new ArrayList<Integer>();
public ArrayList default1() {
vector1.add(1);
vector1.add(2);
vector1.add(3);
return vector1;
}
public ArrayList default2() {
vector2.add(4);
vector2.add(5);
vector2.add(6);
return vector2;
}
public void printVectorContent(ArrayList<Integer> v1) {
for (int i : v1) {
System.out.println(i);
}
}
public int dot(ArrayList<Integer> v1, ArrayList<Integer> v2) {
int product = 0;
for (int i = 0; i < v1.size(); i++) {
product += v1.get(i) * v2.get(i);
}
return product;
}
public static void main(String[] args) {
Vectors test = new Vectors();
System.out.println("Vector 1: ");
test.printVectorContent(test.default1());
System.out.println("Vector 2: ");
test.printVectorContent(test.default2());
System.out.println("Dot Product: ");
System.out.println(test.dot(test.vector1, test.vector2));
}
}
Vectors.main(null);