29. september 2007 - 16:45
Der er
1 kommentar og
1 løsning
Hjælp en java nybegynder.
Har fået en opgave for hvor jeg skal lave en sorterings maskine hvor man så skal kunne søge i en fast array men det virker ikke lige.
Nu er jeg løbet tør for ideer..
kode:
public class sort
{
// instance variables - replace the example below with your own
private int[] int1;
/**
* Constructor for objects of class sort
*/
public void sorter(int[] array)
{
int first = 0;
for (int unsorted = first +1; unsorted < array.length; unsorted++ ){
int value = array[unsorted];
int sorted = unsorted -1;
while ( sorted >= first && value < array[sorted]) {
array[sorted + 1] = array[sorted];
sorted = sorted - 1;
array[sorted + 1] = value;
}
}
}
public int binarySearch(int[] array, int intSearchedFor)
{
int first = 0;
int last = array.length;
int low = first;
int high = last;
while (low <= high)
{
int middle = (low + high) / 2;
if(array[middle] > intSearchedFor) {
high = middle - 1;
}
else if (array[middle] < intSearchedFor){
low = middle + 1;
}
else{
return middle;
}
}
return not found;
}
}
den bliver ved med at sige den mangler et ;
29. september 2007 - 16:48
#1
her er koden hvor jeg skulle kunne hente den ovenstående kode:
kode:
public class test
{
// instance variables - replace the example below with your own
private int [] int1;
private sort sorter;
private sort search;
private int [] intSearchedFor;
/**
* Constructor for objects of class test
*/
public test()
{
//int1 = new int[]{-4,5,8,1,4,-2,-8};
int1 = new int[]{4,1,-8};
sorter = new sort();
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void print()
{
// put your code here
System.out.println("Listen:");
for (int int1Item : int1)
System.out.println("" +int1Item+ "");
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void sort()
{
// put your code here
sorter.sorter(int1);
}
public void search()
{
for (int search : intSearchedFor)
System.out.println("" +search+ "");
}
}