Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Classe che usa un array

    Vi sembra implementata bene questa classe?

    In un metodo main, utilizzando questa classe e facendo un inserimento mi va fuori dall'array, la classe vi sembra ben fatta?

    Si tratta di una classe Insieme che č un array, e all'interno posso inserire, togliere elementi di tipo Giocatori, e ho un metodo che mi restituisce l'indice in cui si trova il giocatore con il punteggio pił alto.

    codice:
    class Insieme{
    
    	Giocatori[] insieme;
    	int lunghezza;
    	Giocatori element;
    	
    	public Insieme(int lungezza){
    		insieme=new Giocatori[lunghezza];
    		this.lunghezza=lunghezza;
    		element=null;
    	}
    	
    	public void add(Giocatori g){
    		int i=0;
    		
    		while(insieme[i]!=null)      //ho un arrayIndexOutOfBounds in questo controllo
    			i++;
    		insieme[i]=g;
    	}
    	   	
    	public Giocatori remove(int index){
    		int i=0;
    		Giocatori tmp=null;
    		while(i!=index)
    			i++;
    		
    		if(i==lunghezza-1)
    		{
    			tmp=insieme[i];
    			insieme[lunghezza-1]=null;
    			return tmp;
    		}
    		
    		else{
    			    tmp=insieme[i];
    			
    				for(int j=i+1; j<lunghezza; j++)
    				{
    					insieme[i]=insieme[j];
    					i++;
    				}
    			
    			   insieme[i]=null;
    			}
    			
    		return tmp;
    	}
    	
    	public int getMax(){
    		int max=insieme[0].p;
    		int index=0;
    		
    		for(int i=0; insieme[i]!=null; i++)
    		{
    			if(max<insieme[i].p){
    				max=insieme[i].p;
    				index=i;
    			}
    		}
    		
    	return index;
    	}
    }

  2. #2
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802

    Re: Classe che usa un array

    Originariamente inviato da Darčios89
    Vi sembra implementata bene questa classe?
    No, gli attributi dovresti dichiararli private.
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  3. #3

    Re: Classe che usa un array

    Originariamente inviato da Darčios89
    Vi sembra implementata bene questa classe?

    In un metodo main, utilizzando questa classe e facendo un inserimento mi va fuori dall'array, la classe vi sembra ben fatta?

    Si tratta di una classe Insieme che č un array, e all'interno posso inserire, togliere elementi di tipo Giocatori, e ho un metodo che mi restituisce l'indice in cui si trova il giocatore con il punteggio pił alto.
    Secondo me puoi fare di meglio!

    Dai un'occhiata al mio esempio:
    codice:
    /**
     * @(#)Insieme.java
     *
     *
     * @author Vincenzo
     * @version 1.00 2010/10/30
     */
    
    public class Insieme {
        
        private Integer insieme[];
        private int length;
        private int n;
        
        /**
         * Creates a new instance of <code>Insieme</code>.
         */
        public Insieme(int length) {
        	this.length = length;
        	insieme = new Integer[length];
        	n = 0;
        }
        
        public void add(Integer obj){
        	if(this.n == this.length)
        		throw new IndexOutOfBoundsException("Array pieno!");
        	
        	insieme[this.n] = obj;
        	this.n++;
        }
        
        public void remove(int index){
        	if(this.n == 0)
        		throw new IndexOutOfBoundsException("Array vuoto!");
        	if(index < 0 || index >= this.length)
        		throw new IndexOutOfBoundsException("Indice non valido!");
        	if(insieme[index] == null)
        		this.n++;
    
        	for(int j=index; j < this.length-1; j++)
        		insieme[j] = insieme[j+1];
        	Integer A[] = new Integer[this.length-1];
        	for(int i=0; i < this.length-1; i++)
        		A[i] = insieme[i];
        	insieme = A;
        	this.length--;
        	this.n--;
        }
        
        public void print(){
        	for(int i=0; i < this.n; i++)
        		System.out.println(insieme[i]);
        }
        
        public Integer max(){
        	if(this.n == 0)
        		throw new RuntimeException("No elements!");
        		
        	Integer max = insieme[0];
        	for(int i=0; i < this.n; i++){
        		if(max.compareTo(insieme[i]) == -1)
        			max = insieme[i];
        	}
        	return max;
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Insieme ins = new Insieme(10);
            ins.add(new Integer(8));
            ins.add(new Integer(9));
            ins.add(new Integer(10));
            ins.print();
            System.out.println("Maximum: " + ins.max());
            System.out.println();
            ins.remove(1);
            ins.add(new Integer(11));
            ins.print();
            System.out.println("Maximum: " + ins.max());
        }
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.