Ginger Posted November 10, 2007 In Java I have an ArrayList of objects, each object consists of two Strings and an integer, I want to create an arrayList of the first String from each object, but not sure how to index an object withinin an array with java. Can anyone help me plx Share this post Link to post Share on other sites
Nappi Posted November 10, 2007 So you have an ArrayList<Object> and you want to create an ArrayList<String> where the strings are determined by the objects in the first ArrayList? Or something else? Maybe something like this will work: ArrayList<String> newList = new ArrayList<String>(); for (Object current : this.listOfObjects) { String textToAdd = current.textOne; newList.add(textToAdd); } I actually don't know shit about coding, though. Share this post Link to post Share on other sites
eljay Posted November 10, 2007 Ok so you have an ArrayList, I'll call it objArray, and each object in the list has string s1, string s2 and int x. Your new ArrayList I'll call stringArray. for (int i = 0; i < objArray.Length; i++) stringArray = objArray.s1; I think that's the kind of thing you're after, there's probably more efficient ways of doing it though. Share this post Link to post Share on other sites
toblix Posted November 10, 2007 Okay, there's a big difference between ArrayLists and arrays. If you're really just using ArrayLists, you could do something like this: ArrayList<String> result = new ArrayList<String>(); for(WeirdObject obj : listOfWeirdObjects){ result.add(obj.getFirstString()); } Of course, if you're not using generics (the <> parts) or if you're using arrays that won't work. It would help if you posted some code. Share this post Link to post Share on other sites
Ginger Posted November 10, 2007 Cheers all three look as tho they would work, I'll have to slip them in and have a play, and hopefully learn. Cheers to you all once again Share this post Link to post Share on other sites
Ginger Posted November 10, 2007 I'm having some probably implementing your methods , it could because the name of the string isn't well defined here is most of the code from the class I need this to happen in, In hte mathematical programs I've used in the past I would ahve used something like array[,2] to get the 2nd coloumn of a matrix, is there anyway like /** * This method computes the Levenshtein distance * @param from first of the two strings to be compared * @param to second string to be compared */ private WordDistance getEditDistance (String from, String to) { int distance; WordDistance wordDistance; distance = editDistance(from,to); wordDistance = new WordDistance(from, to, distance); return wordDistance; } /** * This method returns an ArrayList of WordDistance objects, one for * each of the words in the WordsList field of the Lexicon class * @param word word to be compared to the lexicon items */ private ArrayList<WordDistance> getEditDistances (String word) { ArrayList<WordDistance> wordDistances = new ArrayList<WordDistance>(); for (String compareWord : wordList) wordDistances.add(getEditDistance(word, compareWord)); return wordDistances; } /** * This method removes an entry from an array list and returns the modified * array list * @param word signifies the word of the entry to be removed * @param arrayList the array List from which the 1st entry containing word * will be removed */ private ArrayList<WordDistance> removeEntry(int indexNumber, ArrayList<WordDistance> arrayList) { arrayList.remove(indexNumber); return arrayList; } /** * This method will return an array List of Word Distances with size the number of alternative * words (determined by a parameter) that are most similar to the word entered as a parameter. * The most similar will be near the top * @param numberOfSugests the number of alternative words to be delievered * @param word the word that will be comapared to the others. */ public ArrayList<WordDistance> compileListOfSuggestions(int numberOfSugests, String word) { int noOfSugests = numberOfSugests; ArrayList<WordDistance> arrayList= getEditDistances (word); ArrayList<String> suggestedWords = new ArrayList<String>(); int i=0; while(i<noOfSugests) { int indexNumber= arrayList.indexOf(getMinDistance(arrayList)); \* this where I'm trying to add this *\ suggestedWords.add(arrayList[indexNumber].compareWord); removeEntry(indexNumber, arrayList); i++; } return suggestedWords; } /** * This method receives an ArrayList of WordDistance objects as * parameter and returns the WordDistance object with the minimum * edit distance. If there are several matches, it returns the one * with the lowest index * @param wordDistances ArrayList of WordDistance to be inspected */ private WordDistance getMinDistance (ArrayList<WordDistance> wordDistances) { WordDistance wordDistance = wordDistances.iterator().next(); WordDistance minWordDistance = wordDistance; for (Iterator<WordDistance> i = wordDistances.iterator();i.hasNext() { wordDistance = i.next(); if (minWordDistance.getDistance() > wordDistance.getDistance()) minWordDistance = wordDistance; } return minWordDistance; } Share this post Link to post Share on other sites
toblix Posted November 10, 2007 Oh, sweet! Okay, some comments: The removeEntry() method is just useless. Instead of this: removeEntry(arrayList, i); Just do this: arrayList.remove(i); ... and delete the removeEntry() method. The method you're having problem with has this signature: public ArrayList<WordDistance> compileListOfSuggestions(int numberOfSugests, String word) This means that it's going to retun a list of WordDistance objects. However, later you try to return suggestedWords, which is a list of Strings. That's no good. In the method comment (which is great that you're writing!) it says it will return a list of WordDistances. If that's true, I guess you really want to add the WordDistances from arrayList (terrible variable name, by the way) rather than just the string values? I'm thinking something like this, but I may be misunderstanding the point of the code: public ArrayList<WordDistance> compileListOfSuggestions(int numberOfSugests, String word) { ArrayList<WordDistance> arrayList= getEditDistances (word); ArrayList<WordDistance> suggestedWords = new ArrayList<WordDistance>(); for(int i = 0; i <= Math.min(numberOfSugests, arrayList.size() - 1); i++) { suggestedWords.add(arrayList.get(i)); } return suggestedWords; } If this doesn't work, or if I misunderstood something, let me know. edit: Also, lines like this: int noOfSugests = numberOfSugests; ...are of no use if you're not going to use the original value for something else. Share this post Link to post Share on other sites
Ginger Posted November 10, 2007 Again cheers for the help, the poor choice in variable name is the fault of my lecturer. The reason for the discrepancy between what I return and stated in the signature of the statement is that originally constructed it to return a WordDistance, then changed it and forgot to change alter the signature So the "suggestedWords" Array has objects "WordDistance" and at some point (either now or in the method in another class that calls this method) I want to extract one of the Strings from each of the "WordDistnce" objects in "suggestedWords." But I don't know how to select that String, I tired Eljay's technique of String=suggestedWords.String2, but the problem I think I'm having is not knowing how to refer to the String2. I tried "to" and "compareWord" both terms that are used to refer to what I want in local variables but that didn’t seem to work. Sorry if I'm asking my questions poorly I'm a complete newb at programming and Java. Share this post Link to post Share on other sites
Ginger Posted November 10, 2007 Ahh I forget that there was a class of WordDistance I should be able to find what to call the the String I want from there. Share this post Link to post Share on other sites
Ginger Posted November 10, 2007 ah I had to write an accessor method for the wordDistance class, and hopefully I will now be done. Share this post Link to post Share on other sites
toblix Posted November 10, 2007 Great! If you have any more questions, fire away! Share this post Link to post Share on other sites
Spaff Posted November 11, 2007 jesus this thread scares me Share this post Link to post Share on other sites
elmuerte Posted November 11, 2007 Jesus (or John as he usually calls himself) can't help you with Java Share this post Link to post Share on other sites
DanJW Posted November 12, 2007 No, he prefers perl. Share this post Link to post Share on other sites
elmuerte Posted November 12, 2007 really? I know god used Perl to hack together the universe. But lisp might have been the better choice. Share this post Link to post Share on other sites