[NB Mesmo:)] Coparando Objetos, e um probleminha chato =)

1 resposta
S

Oi :),

Comecei a aprender java na faculdade e o GUJ foi recomendado a turma pelo professor, o mesmo q mandou fazer um exercicio simples de comparação de arrays segue um codigo

e por favor perdoem todas as violações a POO =)

package principal;

public class Main {

	private int[] vetor;
	private int valor;
	private int i;
	private int j;
	
	public void setValor(int pValor){
		for ( i=1 ; i<=10 ;  i++ ){
		this.valor=pValor;
		this.vetor[i]=this.valor;
		}
	}
	
	public int getVetor(int i){
		this.vetor[i]=j;
		return j;
	}
	
        //PUBLIC BOOLEAN COMPARAR:
        //até onde eu posso perceber, esse codigo ficou bem estranho
        //de qualquer forma vale comentar.
        //existe uma função (por favor tomates não!!!) para comparar 2 
        //arrays em duas instancias de uma mesma classe
        // e por ultima mas nao menos importante isso funciona O.o??

	public boolean comparar(int pVetor1[],int pVetor2[]){
		for ( i = 1 ; i <= 10 ; i++ ){
			for ( j = 1 ; j <= 10 ; j++ ){
				if (pVetor1[i] == pVetor2[j] ){
				return false;
				}
			}
		}
		return true;
	}
	
	public Main() {
		
		
	};

	public static void main(String[] args) {
		Main Vetor1 = new Main();
		Main Vetor2 = new Main();
		
		int i;                  
		int a;                  
		int[] b;          
                                                
                                       
                                     
		a=1;
		for (i=1; i<=10 ; i++){
		a+=a;
		Vetor1.setValor(a);
		}
		
		i=1;
		a=1;
		for (i=1; i<=10 ; i++){
			a+=a;
			Vetor2.setValor(a);
		}
		
		i=1;
		a=1;
		b[1]=0;
		for (i=1; 1<=10 ; i++){
			b[i]=getVetor(i);        //AQUI APARECE O ERRO
                                                        //"cannot make a static reference to 
                                                        //a non-static method"
                                                        //isso também não deveria estar aqui?
		}

	}

}

1 Resposta

fmeyer

isso acontece pq vc ta referenciando um objeto nao estatico dentro de um metodo statico …

anyway …

da uma olhada nesse codigo antes de vc continuar …

package org.fmeyer.tmp;

/**
 * Created by IntelliJ IDEA.
 * User: Fernando
 * Date: Aug 20, 2006
 * Time: 8:14:41 PM
 */

class Tvector {
    private int[] _internalBuffer;
    private int   _internalvalue;
    private int   _internalsize;

    public Tvector(int size) {
        this._internalsize = size;
        _internalBuffer = new int[size];
    }

    public void setValor ( int valor, int pos) {
        if (pos &gt this._internalsize)
            throw new ArrayIndexOutOfBoundsException();
        this._internalBuffer[pos] = valor;
    }

    public int getValor( int pos ){
        if (pos &gt this._internalsize)
            throw new ArrayIndexOutOfBoundsException();
        return this._internalBuffer[pos];
    }

    public boolean equals(Tvector valor) {
        for ( int i = 0; i &lt _internalsize; i++ ){
            if ( this._internalBuffer[i] != valor.getValor(i) )
                return false;
        }
        return true;
    }
}

public class TmpClass {
    public static void main( String args[] ){

        int chargeValue = 1;

        Tvector array1 = new Tvector(10);
        Tvector array2 = new Tvector(10);

        for (int i = 0; i &lt 10; i++) {
            chargeValue += chargeValue;
            array1.setValor(chargeValue, i);
            array2.setValor(chargeValue, i);
        }
        System.out.print(array1.equals(array2));
    }
}
Criado 20 de agosto de 2006
Ultima resposta 20 de ago. de 2006
Respostas 1
Participantes 2