Foros del Web » Programación para mayores de 30 ;) » Java »

Problema de ArrayList y binarySearch()

Estas en el tema de Problema de ArrayList y binarySearch() en el foro de Java en Foros del Web. Hola por favor necesito que alguien me de una respuesta tengo estas clases y me da un error al usar binarySearch(); por si acaso ya ...
  #1 (permalink)  
Antiguo 23/11/2009, 13:09
 
Fecha de Ingreso: noviembre-2007
Mensajes: 28
Antigüedad: 16 años, 5 meses
Puntos: 0
Exclamación Problema de ArrayList y binarySearch()

Hola por favor necesito que alguien me de una respuesta
tengo estas clases y me da un error al usar binarySearch();
por si acaso ya revise la api y todo.
aca les dejo las clases necesarias


Con esta clase leo un archivo y lo almaceno en un arraylist
Código:
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.List;


public class LeeArchivo {

ArrayList promedios=new ArrayList();
ArrayList<Alumno> lista=new ArrayList<Alumno>();
String nombre="",rut="",nota1="",nota2="",nota3="",lab1="",lab2="",lab3="";
int largo=0;
public LeeArchivo(){
        File file = new File("C:/prueba.txt");
        FileInputStream fis = null;

       // Alumno[] al=new Alumno[10];
		int j=0;
		double nota1f,nota2f,nota3f,lab1f,lab2f,lab3f;

        BufferedInputStream bis = null;
        DataInputStream dis = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            dis = new DataInputStream(bis);


            while (dis.available() != 0) { //lee todas lias lineas del archivo cuando es diferente de cero tiene datos
                 String linea = dis.readLine(); // se guardan los datos de la linea en el objeto String linea.

                 //System.out.println(linea);    //se imprime el valor de linea

                 StringTokenizer st = new StringTokenizer(linea," "); // se divide la variable linea en tokens o palabras que estan separadas por un espacio


                 String []datos = new String[st.countTokens()]; //declaramos un arreglo de Strings para guardar las cadenas
                 int i=0;

                while(st.hasMoreTokens()){ // sirve para obtener los tokens de st

                     datos[i]=st.nextToken(); // los guardo en el arreglo
                      nombre=datos[0];
                      rut=datos[1];
                      nota1=datos[2];
                      nota2=datos[3];
                      nota3=datos[4];
                      lab1=datos[5];
                      lab2=datos[6];
                      lab3=datos[7];
                   
                     i++;

                }
               lista.add(new Alumno(nombre,rut,nota1,nota2,nota3,lab1,lab2,lab3));
               Alumno alum=new Alumno();
				Collections.sort(lista);
                System.out.println(lista);
                int jj=Collections.binarySearch(lista,"fd");// AQUI EL ERROR
			String n1=lista.get(j).getNota1();
			nota1f=Double.valueOf(n1).doubleValue();

			String n2=lista.get(j).getNota2();
			nota2f=Double.valueOf(n2).doubleValue();

			String n3=lista.get(j).getNota3();
			nota3f=Double.valueOf(n3).doubleValue();
                
                String l1=lista.get(j).getLab1();
				lab1f=Double.valueOf(l1).doubleValue();

				String l2=lista.get(j).getLab2();
				lab2f=Double.valueOf(l2).doubleValue();

				String l3=lista.get(j).getLab3();
				lab3f=Double.valueOf(l3).doubleValue();



		double promedio=(((nota1f+nota2f+nota3f)/3)*0.5)+
                        (((lab1f+lab2f+lab3f)/3)*0.5);
                promedios.add(promedio);
				lista.get(j).setPromedio(promedio);
				j++;
            }
            largo=j;
           

            //System.out.println(lista);


        } catch (FileNotFoundException e) {


            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Aca esta la clase Alumno
Código:
import java.io.*;
import java.util.*;
import java.util.Collections;

public class Alumno implements Comparable<Alumno> {

	String nombre;
	String rut;
	String nota1;
	String nota2;
	String nota3;
    String lab1;
    String lab2;
    String lab3;
	double promedio;

	public Alumno(String nombre,String rut,String nota1,
                  String nota2,String nota3,String lab1,
                  String lab2,String lab3)
	{
		this.nombre=nombre;
		this.rut=rut;
		this.nota1=nota1;
		this.nota2=nota2;
		this.nota3=nota3;
        this.lab1=lab1;
        this.lab2=lab2;
        this.lab3=lab3;
		this.promedio=0;


	}

	public Alumno()
	{}

	public void setNombre(String nombre)
	{this.nombre=nombre;}

	public String getNombre()
	{return nombre;}

	public void setRut(String rut)
	{this.rut=rut;}

	public String getRut()
	{return rut;}

	public void setNota1(String nota1)
	{this.nota1=nota1;}

	public String getNota1()
	{return nota1;}

	public void setNota2(String nota2)
	{this.nota2=nota2;}

	public String getNota2()
	{return nota2;}

	public void setNota3(String nota3)
	{this.nota3=nota3;}

	public String getNota3()
	{return nota3;}

    public void setLab1(String lab1)
	{this.lab1=lab1;}

	public String getLab1()
	{return lab1;}

    public void setLab2(String lab2)
	{this.lab2=lab2;}

	public String getLab2()
	{return lab2;}

    public void setLab3(String lab3)
	{this.lab3=lab3;}

	public String getLab3()
	{return lab3;}

	public void setPromedio(double promedio)
	{this.promedio=promedio;}

	public double getPromedio()
	{return promedio;}

	public String toString()
	{
	 return nombre+" "+
	 rut+" "+
	 nota1+" "+
	 nota2+" "+nota3+" "+lab1+" "
     +lab2+" "+lab3;
	}
    public int compareTo(Object o) {
	    Alumno otroUsuario = (Alumno) o;
	    //podemos hacer esto porque String implementa Comparable
	    return nombre.compareTo(otroUsuario.getNombre());

}

    public int compareTo(Alumno o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
De antemano muchas gracias
  #2 (permalink)  
Antiguo 24/11/2009, 02:22
Avatar de elAntonie  
Fecha de Ingreso: febrero-2007
Mensajes: 894
Antigüedad: 17 años, 2 meses
Puntos: 10
Respuesta: Problema de ArrayList y binarySearch()

Wenas

Acabo de ver este hilo desde el otro.

Tienes que tener en cuenta que el compareTo de la clase String, no funciona como tu crees, ya que compara primero por tamaño.

Es decir Juan (4 caracteres) es menor que Gabriel (7). En igualdad de tamaño, si que lo hace por orden alfabetico.

De todos modos, cual es el error que te da??

Saludos.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:00.