Java tutorial
/* * Copyright 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu> * * This file is part of UtilPerseus. * * UtilPerseus is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * UtilPerseus is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with UtilPerseus. If not, see <http://www.gnu.org/licenses/>. */ //package eu.himeros.util; import java.util.Vector; /** * Simulate a matrix. Provides method to travers vectors that compose the * matrix. * * @author federico */ public class Matrix { private Vector<Integer>[] v = null; private final int sizeDefault = 1000; private int dim = 2; /** * Default constructor. */ public Matrix() { init(dim, 1000); } /** * Init the matrix with dimension. * * @param dim * dimension. */ public Matrix(int dim) { this.dim = dim; init(dim, 1000); } /** * Constructor that init the matrix with number of dimensions and size. * * @param dim * dimension. * @param size * size. */ public Matrix(int dim, int size) { init(dim, sizeDefault); } /** * Init the matrix with number of dimensions and size. * * @param dim * dimension. * @param size * size. */ public void init(int dim, int size) { v = new Vector[dim]; for (int i = 0; i < dim; i++) { v[i] = new Vector<Integer>(size); } } /** * Add an array of integers, traversing all the vectors. * * @param x * the integer array to add. */ public void add(int[] x) { for (int i = 0; i < x.length; i++) { v[i].add(x[i]); } } /** * Add integers to the first two vectors. * * @param x1 * integer to add to the first vector. * @param x2 * integer to add to the second vector. */ public void add(int x1, int x2) { // for bi-dimensional arrays v[0].add(x1); v[1].add(x2); } /** * Return an int array, traversing all the vectors at the given index. * * @param idx * the index. * @return the int array. */ public int[] get(int idx) { int[] res = new int[dim]; for (int i = 0; i < dim; i++) { res[i] = v[i].get(idx); } return res; } /** * Remove an item in all the vectors at the given index. * * @param idx * the index. */ public void remove(int idx) { for (int i = 0; i < dim; i++) { v[i].remove(idx); } } /** * Get the vector array. * * @return the vector array. */ public Vector[] getVectorArray() { return v; } /** * Get size of the matrix. * * @return size. */ public int size() { return v[0].size(); } }