The Artima Developer Community
Sponsored Link

Java Answers Forum
how do you get an array to contain results from another array

1 reply on 1 page. Most recent reply: Mar 29, 2006 3:36 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Michelle Loh

Posts: 10
Nickname: modernage
Registered: Feb, 2006

how do you get an array to contain results from another array Posted: Mar 27, 2006 5:35 PM
Reply to this message Reply
Advertisement
A ResultSet object will hold the results of a web search. Each result, or "hit" (web page that matches terms we searched for), will be represented by a Hit object. Each Hit object records the title and web address of one web page that matched our search criteria. A ResultSet should maintain a collection of these Hit objects.

when i run the driver SearchUI to give me ten results of a search term, it gives me 10 results, 0 through 9 and each says null (this is incorrect)..what is wrong in my ResultSet code?


this is my ResultSet class:

public class ResultSet
{
private Hit[]hit,sortedresults;
private int numhits,i,j;


public ResultSet(int maxsize)
{
hit= new Hit[maxsize];
numhits=0;
}

public boolean addResult(Hit result)
{
if (numhits==hit.length)
{
return false;
}
hit[numhits]=result;
numhits++;
return true;
}

public int getSize()
{
return numhits;
}

public Hit getResult(int i)
{
if (hit.length<i+1)
{
return null;
}
return hit[i];
}

public boolean getSortedResults(int first, int last, boolean bytitle,
Hit[] sortedresults)
{
if (bytitle)
{
for (int sortedsize=0; sortedsize<numhits-1; sortedsize++)
{
int earliestpos=sortedsize;
for (int pos=earliestpos+1; pos<numhits; pos++)
{
if (hit[pos].compareByTitleTo(hit[earliestpos])<0)
{
earliestpos=pos;
}
}
Hit a=hit[sortedsize];
hit[sortedsize]=hit[earliestpos];
hit[earliestpos]=a;
sortedresults=new Hit[last-first+1];
j=0;
for (int i=first; i<last+1; i++)
{
sortedresults[j]=hit[i];
j++;
}
return true;
}
}
else
{
for (int sortedsize=0; sortedsize<numhits-1; sortedsize++)
{
int earliestpos=sortedsize;
for (int pos=earliestpos+1; pos<numhits; pos++)
{
if (hit[pos].compareByAddressTo(hit[earliestpos])<0)
{
earliestpos=pos;
}
}
Hit a=hit[sortedsize];
hit[sortedsize]=hit[earliestpos];
hit[earliestpos]=a;
sortedresults=new Hit[last-first+1];
j=0;
for (int i=first; i<last+1; i++)
{
sortedresults[j]=hit[i];
j++;
}
return true;
}
}

return false;
}


}

and this is the Hit class:

public class Hit
{
private static int comparisons = 0;

public static int getComparisons()
{
return comparisons;
}

public static void resetComparisons()
{
comparisons = 0;
}



private String title, address;

public Hit(String title, String address)
{
this.title = title;
this.address = address;
}

public void print()
{
System.out.println(title);
System.out.println(address);
}

public int compareByTitleTo(Hit other)
{
comparisons++;
return this.title.compareToIgnoreCase(other.title);
}

public int compareByAddressTo(Hit other)
{
comparisons++;
return this.address.compareToIgnoreCase(other.address);
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: how do you get an array to contain results from another array Posted: Mar 29, 2006 3:36 AM
Reply to this message Reply
Please format your code using the java tags as shown on the right side of the input field (Formatting Your Post).

It's really hard to read code like that.

The method getSortedResults has an error.

You pass an array to the method, inside this method you create a new array but this way the result is lost once the method is finished.
You cave 2 possibilities:
1. Fill the original array
2. return the new array instead of the boolean value.

Flat View: This topic has 1 reply on 1 page
Topic: Problem with files Previous Topic   Next Topic Topic: Best practices for evolving existing J2EE code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use