Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Unsorted array list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unsorted array list" Watch "Unsorted array list" New topic
Author

Unsorted array list

Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
I have an unsorted string array list that I must return the smallest array.

public String smallest () {
//Returns smallest element in the list
String smallest = list[0];
for (int i = 1; i < list.length; ++i) {
if (list[i] < smallest) {
smallest = list[i];
}
}
return new String (list[0]);

}
}
I get an error: operator > cannot be applied to Java.lang.String...

Thanks

Steph
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you're looking for the "shortest" String, then you will want to call the length() method on your Strings and compare these int values.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
Marc,

Can you be more specific?

Steph
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

String a = "hello";
int size = a.length();


size will be 5 for how long the "hello" String is.

you cannot compare strings with the less than or greater than sign, it is not allowed.

so if you just change one line of your code the one with the "<" to have each String call its length() method, then you are working.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ricky Martaputra
Ranch Hand

Joined: Apr 04, 2005
Posts: 42
Hi,

I don't really understand what order do you want to be sorted, but i think, if you want to compare the string, not the length of the string, you can use compareTo method.

Example:
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
I have an unsorted string array list that I must return the smallest array.

Do you mean the smallest element in the ArrayList?



//Returns smallest element in the list

Does "smallest element" mean "shortest String"?


if (list[i] < smallest)
I get an error: operator > cannot be applied to Java.lang.String...


"list[i]" is a String. "smallest" is a String.

if (String < String)

is not valid syntax in Java. For example
if ("hello" < "goodbye")
is not valid.

The '<' operator is valid for numbers. If you are looking for the shortest String, you need to find out the length of each String in the list. The way you find the length of a String is by using the length() method of String. For example,
int helloLength = "hello".length();
The length of "hello" is 5.

If you mean something else by "smallest element", please be more specific.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
I need the smallest element in the array.

The array list is:

beta
gamma
alpha
delta

The result should be alpha.
Jody Brown
Ranch Hand

Joined: Nov 09, 2005
Posts: 43
So by smallest, I presume you mean in alphabetic order? Because the smallest in length in the list you give is "beta". If that is the case, your easiest option might be to just sort the array into alphabetical order and return the first element. Or, if this isn't feasible, you could do character comparisions. There may be a method out there to compare two strings and return the one which should come first, but I dont know of any right off the top of my head without digging into the API.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Ricky already gave you that method: compareTo. compareTo (and compareToIgnoreCase) compares strings lexographically - so alpha comes before beta, and abc comes before abcd.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
I got it to compile, but got this error.

Exception in thread "main" java.lang.NullPointerException
at UnsortedStringList.smallest(UnsortedStringList.java:107)
at TestUnsortedStringList1.main(TestUnsortedStringList1.java:23)

Code:
public String smallest ()
//Returns smallest element in the list
{
String smallest = list[0];
for (int i = 1; i < list.length; ++i) {
if(list[i].compareTo(smallest) >0) {
smallest = list[i];
}
}
return new String (list[0]);

}
}
Jody Brown
Ranch Hand

Joined: Nov 09, 2005
Posts: 43
It looks like your list is empty, at least in part. Make sure you have initialised it properly with the strings you want to compare and that it doesnt contain any gaps (nulls).
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67

public String smallest ()
//Returns smallest element in the list
{
String smallest = list[0];
for (int i = 1; i < list.length; ++i) {
if(list[i].compareTo(smallest) >0) {
smallest = list[i];
}
}
return new String (list[0]);

}
}


this method will return list[0], not smallest, or will it?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
What does your main method look like?
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
Maybe this will also help. This is my assignment:

The Unsorted List ADT (for UnsortedStringList) is to be extended with an operation, smallest, which returns a copy of the "smallest" list element. It is assumed that the operation will not be invoked if the list is empty.

Write a method to implement the operation. The method should be added to the attached file UnsortedStringList.java.

The result is to be: The smallest list element is: alpha

This is the main method:

// class TestUnsortedStringList1
// This test program tests the 'smallest' operation

public class TestUnsortedStringList1
{
public static void main(String[] args)
{
UnsortedStringList usl = new UnsortedStringList();

// insert some list elements
usl.insert("beta");
usl.insert("gamma");
usl.insert("alpha");
usl.insert("delta");

// determine the smallest element
String smallestElement = usl.smallest();
System.out.println("The smallest list element is: " + smallestElement);
}
}

Below is the entire file that goes with my code:

public class UnsortedStringList

{
protected String[] list; // Array to hold list elements
protected int numItems; // Number of elements in this list
protected int currentPos; // Current position for iteration

public UnsortedStringList(int maxItems)
// Instantiates and returns a reference to an empty list object with room for maxItems elements
{
numItems = 0;
list = new String[maxItems];
}

public UnsortedStringList()
// Instantiates and returns a reference to an empty list object with room for 100 elements.
{
numItems = 0;
list = new String[100];
}

public boolean isFull()
// Returns whether this list is full.
{
return (list.length == numItems);
}

public int lengthIs()
// Returns the number of elements in this list.
{
return numItems;
}

public boolean isThere (String item)
// Returns true if item is on this list, otherwise returns false.
{
boolean moreToSearch;
int location = 0;
boolean found = false;

moreToSearch = (location < numItems);

while (moreToSearch && !found)
{
if (item.compareTo(list[location]) == 0) // if they match
found = true;
else
{
location++;
moreToSearch = (location < numItems);
}
}

return found;
}

public void insert (String item)
// Adds a copy of item to this list.
{
list[numItems] = new String(item);
numItems++;
}

public void delete (String item)
// Deletes the element that matches item from this list.
{
int location = 0;

while (item.compareTo(list[location]) != 0)
location++;

list[location] = list[numItems - 1];
numItems--;
}

public void reset()
// Initializes current position for an iteration through this list.
{
currentPos = 0;
}

public String getNextItem ()
// Returns copy of the next element in this list.
{
String next = list[currentPos];
if (currentPos == numItems-1)
currentPos = 0;
else
currentPos++;
return new String(next);
}

public String smallest ()
//Returns smallest element in the list
{
String smallest = list[0];
for (int i = 1; i < list.length; ++i) {
if(list[i].compareTo(smallest) > 0) {
smallest = list[i];
}
}
return new String(list[0]);

}
}
[ November 12, 2005: Message edited by: Stephanie Dears ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10

The problem is that you have constructed an array of 100 elements, but you have only filled in 4 of those 100 slots with actual content. The other 96 are still null. So when you try to compare list[4], which is null, to "smallest", you get a NullPointerException. You might want to check for "null" elements before you do the compare.
[ November 12, 2005: Message edited by: Marilyn de Queiroz ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unsorted array list