Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Please Help, should be working but isn't
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
EricUnreal




PostPosted: Thu Jan 26, 2012 11:19 pm   Post subject: Please Help, should be working but isn't

My final is tommorrow and i'm having trouble with some questions that are similar to those that will be on the exam. Here's what i have:


I have to insert a word into the list while maintaining alphabetical order. What i'm trying to do is insert the word, then just re-sort the list.

import java.util.*;
import java.io.*;

public class WordList
{
private ArrayList<String> list;

public WordList()
{
list = new ArrayList<String>();
getData();
}
public void addWord(String aWord)
{
list.add(aWord);
for (int current=0; current<list.length; current++) {
int lowPos = current;
for(int i=current; i<list.length; i++) {
if( list[i].compareTo(list[lowPos])<0 ) {
lowPos = i;
}
}
String temp = list[current];
list[current] = list[lowPos];
list[lowPos] = temp;
}
}
}
Sponsor
Sponsor
Sponsor
sponsor
EricUnreal




PostPosted: Thu Jan 26, 2012 11:25 pm   Post subject: RE:Please Help, should be working but isn\'t

To clarify, it's giving me an error at list.length; which i dont think it should because it is an ArrayList
Tony




PostPosted: Thu Jan 26, 2012 11:49 pm   Post subject: RE:Please Help, should be working but isn\'t

there is no such field in ArrayList
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
EricUnreal




PostPosted: Thu Jan 26, 2012 11:52 pm   Post subject: Re: RE:Please Help, should be working but isn\'t

Tony @ Thu Jan 26, 2012 10:49 pm wrote:
there is no such field in ArrayList


Well what could I use instead? or even in an Array.
Tony




PostPosted: Fri Jan 27, 2012 12:23 am   Post subject: RE:Please Help, should be working but isn\'t

Perhaps you'll find one of the available methods to be suitable? http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: