Sum of two different vectors

0

Help with this code, I need to know what the logical error is.

package vectores;
import java.util.*;
public class Vector {
    private ArrayList<Integer> vector;
    private int numElementos;

    public Vector()
    {
        vector=new ArrayList();
        numElementos=0;
    }

    public Vector(int numElementos)
    {
       this.numElementos = numElementos;
    }

    public int getNumElementos() {
        return numElementos;
    }

    public void setNumElementos(int numElementos) {
        this.numElementos = numElementos;
    }

    public ArrayList<Integer> getVector() {
        return vector;
    }

    public void setVector(ArrayList<Integer> vector) {
        this.vector = vector;
    }

    public void agregarElemento(int el)
    {
        vector.add(el);
    }

    public void borrarTodoElementos()
    {
        vector.clear();
    }

    public Vector suma(Vector v2,Vector v1)
    {
         Vector v3=new Vector();
         int i=0;
         for(int x=0;x<=v2.getVector().size();x++)
         {
            i=v1.vector.get(x)+v2.vector.get(x);
         }

         for(int x=0;x<=v2.getVector().size();x++)
         {
             v3.vector.add(i);
         }
         return v3;
    }

    public Vector resta(Vector v2,Vector v1)
    {
         Vector v3=new Vector();
         int i=0;
         for(int y=0;y<=v2.getVector().size();y++)
         {
            i=v1.vector.get(y)-v2.vector.get(y);
         }

         for(int x=0;x<=v2.getVector().size();x++)
         {
             v3.vector.add(i);
         }
         return v3;
    }

    public Vector productoVectorxNumero(int numero)
    {
        Vector v2=new Vector();
        Vector v3=new Vector();
        int i=0;
        for(int y=0;y<=v2.getVector().size();y++)
        {
           i=v2.vector.get(y)*numero;
        }

        for(int x=0;x<=v2.getVector().size();x++)
         {
             v3.vector.add(i);
         }
         return v3; 
    }


    public String toString()
    {
        Vector v2=new Vector();
        for(int x=0;x<=v2.getVector().size();x++)
            return v2.vector.get(x).toString();
        return null;
    }

}
package vectores;
import java.io.*;
public class Vectores {

     public int menu()
    {
        int op=0;
        BufferedReader teclado=new BufferedReader (new InputStreamReader (System.in));
        try{
        System.out.println("1.Sumar vectores");
        System.out.println("2.Restar vectores");
        System.out.println("3.Escalar un vector");
        System.out.println("4.Exit");
        op=Integer.parseInt(teclado.readLine());
        }catch(Exception ex){
            System.out.println("Error: "+ex.getMessage());
        }

        return op;  
    }

    public static void main(String[] args) {
        BufferedReader teclado=new BufferedReader (new InputStreamReader (System.in));
        Vectores o=new Vectores();
        Vector v3=new Vector();
        Vector v1=new Vector();
        Vector v2=new Vector();
        int elemento,e,opc,e2,elemento2;
        try{
            System.out.println("Ingrese la cantidad de elementos del primer vector: ");
            elemento=Integer.parseInt(teclado.readLine());
            v1.setNumElementos(elemento);
            for(int x=1;x<=elemento;x++)
            {
                System.out.println("Ingrese elemento");
                e=Integer.parseInt(teclado.readLine());
                v1.agregarElemento(e);
            }

            System.out.println("Ingrese la cantidad de elementos segundo del vector: ");
            elemento2=Integer.parseInt(teclado.readLine());
            v2.setNumElementos(elemento2);
            for(int x=1;x<=elemento2;x++)
            {
                System.out.println("Ingrese elemento");
                e2=Integer.parseInt(teclado.readLine());
                v2.agregarElemento(e2);
            }
        }catch(Exception ex){
            System.out.println("Error: "+ex.getMessage());
        }

        do{
            opc=o.menu();
            switch(opc){
                    case 1:
                           if(v1.getNumElementos()==v2.getNumElementos())
                           {
                               v3.suma(v2,v1);
                               System.out.println("su vector es:"+v3.toString());
                           }
                           else
                               System.out.println("Error");
                           break;
                    case 2:
                           if(v1.getNumElementos()==v2.getNumElementos())
                           {
                               v3.resta(v2,v1);
                               System.out.println("su vector es:"+v3.toString());
                           }
                           else
                               System.out.println("Error");
                           break;
                    case 3:
                           int n;
                           try{
                                System.out.println("Ingrese número para escalar vector");
                                n=Integer.parseInt(teclado.readLine());
                                v1.productoVectorxNumero(n);
                                System.out.println("su vector es:"+v3.toString());
                           }catch(Exception ex){
                               System.out.println("Error"+ex.getMessage());
                           }
                           break;
                    case 4:
                           break;           
                }
        }while(opc!=4);
    }

}

This is the error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.get(ArrayList.java:433)
    at vectores.Vector.suma(Vector.java:51)
    at vectores.Vectores.main(Vectores.java:60)
C:\Users\Catalina\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 9 seconds)

thanks !!!

    
asked by Carolina Membloc 09.11.2018 в 02:15
source

2 answers

1


Problem

You have errors in the for loops , as you indicate, for example:

for (int x = 0; x <= v2.getVector().size(); x++)

and then you use the value of x to extract the values of the vectors , when you get to the value of x that is equal to the length of the vector the exception jumps:

  

java.lang.IndexOutOfBoundsException

since you are trying to extract a value from the array "out of bounds" of that array .


Explanation

If you have an array with 3 elements and you measure its size, you get 3. To access the values of the array, which in this case has 3, you start at its 0 position, that is, you get its positions: 0, 1 and 2 (3 values). When you match within the for, you indicate that it enters the loop when x is worth 3 and so extracts the value of the array in position 3, which does not exist.


Solution

You can only "extract" from the array its length minus 1 , so replace the < = expression of your for loops. for < , for example:

for (int x = 0; x <= v2.getVector().size(); x++)

for

for (int x = 0; x < v2.getVector().size(); x++)


Note: and besides all this, to begin with, I would implement the arithmetic operations with the Vector class in a separate class.

    
answered by 09.11.2018 в 10:23
0

I have taken a look at your code, the elements of a list in this case a class called ArrayList that is implemented in List, saves the elements from an index 0 to a number - > n - 1 . when you add 2 values [0, 1] then when you define the for loops they start at y = 0, in the condition you put and

answered by 09.11.2018 в 05:33