import java.io.Buffer
edReader;
import java.io.FileRe
ader;
import java.io.IOExce
ption;
import java.io.InputS
treamReader;
// highArray.java
// demonstrates array class with high-level interface
// to run this program: C>java HighArrayApp
////////////////////////////////////////////////////////////////
class HighArray
{
private double[] a; // ref to array a
private int nElems; // number of data items
//----------------------------------------------------------
public HighArray(int max) // constructor
{
a = new double[max]; // create the array
nElems = 0; // no items yet
}
//----------------------------------------------------------
public boolean find(double searchKey)
{ // find specified value
int j;
for(j=0; j<nElems; j++) // for each element,
if(a[j] == searchKey) // found item?
break; // exit loop before end
if(j == nElems) // gone to end?
return false; // yes, can't find it
else
return true; // no, found it
} // end find()
//----------------------------------------------------------
public void insert(double value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//----------------------------------------------------------
public boolean delete(double value)
{
int j;
for(j=0; j<nElems; j++) // look for it
if( value == a[j] )
break;
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move higher ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//----------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//----------------------------------------------------------
} // end class HighArray
public static void main(String[] args) throws IOException
{
int size, n, keysPerCell;
String aKey="";
HighArray = new HighArray();
// get sizes
System.out.print("Tamaño: ");
size = getInt();
System.out.print("Numero de datos que se van a ingresar: ");
n = getInt();
keysPerCell = 10;
// make table
HashTable theHashTable = x.new HashTable(size);
String inputFileName = "Listado de 5000.txt";
try{
// Create FileReader Object
FileReader inputFileReade
r = new FileReader(inputFileName);
BufferedReader inputStream = new BufferedReader(inputFileReade
r);
String inLine = null;
while ((inLine = inputStream.re
adLine()) != null) {
DataItem aDataItem;
aKey = inLine;
aDataItem = x.new DataItem(aKey);
theHashTable.insert(aDataItem);
}
System.out.println("Colisiones: "+x.contadorColision);
}
catch (IOException e){
System.out.println("IOException:");
}
while(true) // interact with user
{
System.out.println("Ingrese la primer letra de lo que desea hacer: \n");
System.out.print("1. Mostrar Tabla(m)\n2. Insertar(i)\n3. Eliminar(e)\n4. Buscar(b)");
char choice = getChar();
switch(choice)
{
case 'm':
theHashTable.displayTable();
break;
case 'i':
System.out.print("Ingrese el valor a insertar: ");
DataItem aDataItem;
aKey = getString();
aDataItem = x.new DataItem(aKey);
long t = System.nanoTim
e();
theHashTable.insert(aDataItem);
long t0 = System.nanoTim
e();
System.out.println("tiempo: ");
System.out.print(t0-t);
System.out.println(" nanosegundos");
break;
case 'e':
System.out.print("Ingresa el valor a eliminar: ");
aKey = getString();
long t1 = System.nanoTim
e();
aDataItem = theHashTable.d
elete(aKey);
long t2 = System.nanoTim
e();
System.out.println("tiempo: ");
System.out.print(t2-t1);
System.out.println(" nanosegundos");
//if (aDataItem == null)
//System.out.println("no existe el dato");
break;
case 'b':
System.out.print("Ingrese el valor a buscar: ");
aKey = getString();
long t3 = System.nanoTim
e();
aDataItem = theHashTable.f
ind(aKey);
long t4 = System.nanoTim
e();
System.out.println("tiempo: ");
System.out.print(t4-t3);
System.out.println(" nanosegundos");
if(aDataItem != null)
{
System.out.println("Encontrado: " + aKey);
}
else
System.out.println("No se pudo hallar el valor " + aKey);
break;
default:
System.out.print("Entrada invalida\n");
} // end switch
} // end while
} // end main()
//--------------------------------------------------------------
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamRea
der(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
//--------------------------------------------------------------
public static char getChar() throws IOException
{
String s = getString();
return s.charAt(0);
}
//-------------------------------------------------------------
public static int getInt() throws IOException
{
String s = getString();
return Integer.parseI
nt(

;
}
//--------------------------------------------------------------
// end class HashTableApp
///////////////////////////////////////////////////////////////
}