Results 1 to 8 of 8
Thread: Question about arraylist
- 10-01-2010, 10:04 PM #1
Question about arraylist
Hi, my question was how does one put objects into an arraylist? I tried using the .add but said incompatible types when I tried to give it an object in the parameter. I don't have any code that would do any good for this question but maybe someone knows how to pass objects to an arraylist. Thanks :)
Also Im reading in lines of data from an external file and trying to make objects of type Country and put them into one of four arraylists depending on country codeLast edited by Metastar; 10-01-2010 at 10:39 PM.
- 10-01-2010, 10:29 PM #2
ill post what I got I guess. Maybe will make sense but I'm not sure
Java Code:import java.io.*; import java.util.*; /** * * @author */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { FileInputStream fis1= new FileInputStream("AsiaCountries.Fall2010.txt"); BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1)); String inline; int totalReg1Counter=0, totalReg2Counter=0, totalReg3Counter=0; int totalReg4Counter=0; ArrayList<String> myArrList1 = new ArrayList<String>(); ArrayList<String> myArrList2 = new ArrayList<String>(); ArrayList<String> myArrList3 = new ArrayList<String>(); ArrayList<String> myArrList4 = new ArrayList<String>(); while ((inline = br1.readLine()) != null) { Country tempCountry = new Country (inline); if(tempCountry.getCountryRegionNum() == 1) { myArrList1.add(totalReg1Counter, inline); totalReg1Counter++; // System.out.println(myArrList1.get(totalReg1Counter)); }// end reg1 if else if(tempCountry.getCountryRegionNum() == 2) { myArrList2.add(totalReg2Counter, inline); totalReg2Counter++; }// end reg2 if else if(tempCountry.getCountryRegionNum() == 3) { myArrList3.add(totalReg3Counter, inline); totalReg3Counter++; }// end reg3 if else if(tempCountry.getCountryRegionNum() == 4) { myArrList4.add(totalReg4Counter, inline); totalReg4Counter++; }//end reg4 if }//end while }//end main() }//end MainJava Code:package project2cquast; /** * * @author */ public class Country { private String countryName, countryCapital, countryRegion, countryAbbrev; private int countryRegionNum; //------------------------------------------------------------------------- //Constructor: Parses data read into Country attributes and assigns // data to respective variables. //------------------------------------------------------------------------- /** * * @param countryInput String variable. * @return none */ public Country(String countryInput) { countryName = countryInput.substring(0,17).trim(); countryCapital = countryInput.substring(21,40).trim(); countryRegion = countryInput.substring(44,55).trim(); countryRegionNum = Integer.parseInt(countryInput.substring(63,65).trim()); countryAbbrev = countryInput.substring(66,68).trim(); }//end parseData Constructor() //------------------------------------------------------------------------- // Country Name accessor. //------------------------------------------------------------------------- /** * @param none * @return Country Name */ public String getCountryName() { return countryName; }//end getCountryName() //------------------------------------------------------------------------- // Country Capital accessor. //------------------------------------------------------------------------- /** * @param none * @return Country Capital */ public String getCountryCapital() { return countryCapital; }//end getCountryCapital() //------------------------------------------------------------------------- // Country Region accessor. //------------------------------------------------------------------------- /** * @param none * @return Country Region */ public String getCountryRegion() { return countryRegion; }//end getCountryRegion() //------------------------------------------------------------------------- // Country Region Number accessor. //------------------------------------------------------------------------- /** * @param none * @return Country Region Number */ public int getCountryRegionNum() { return countryRegionNum; }//end getCountryRegionNum() //------------------------------------------------------------------------- // Country Abbreviation accessor. //------------------------------------------------------------------------- /** * @param none * @return Country Abbreviation */ public String getCountryAbbrev() { return countryAbbrev; }//end getCountryAbbrev() //------------------------------------------------------------------------- // Formats outputs to fit under headers. //------------------------------------------------------------------------- /** *@param none * @return Formatted string outputs */ public String toString() { return String.format("%-23s %-30s %-24s %-16d %-4s\n", countryName, countryCapital,countryRegion, countryRegionNum, countryAbbrev); }//end toString() }Last edited by Metastar; 10-01-2010 at 10:31 PM.
- 10-01-2010, 10:41 PM #3
If you got errors compiling your code, Please copy and paste the full text here.said incompatible types
- 10-01-2010, 11:07 PM #4
I just saw a video that says you have to convert an array to a list and a list to an array depending on what you need to do. Is this true for arraylist? I've just never had to use arraylist before and my professor didn't go over it at all and expects us to just know it. Any way you could maybe explain arraylist to me in relevance to my problem?
Also this is the error I get if I were to uncomment the print in the first of the 4 if statements in the while:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at project2cquast.Main.main(Main.java:40)
Java Result: 1
- 10-01-2010, 11:11 PM #5
Look at the code in your program at line 40.
When that code was executed the index for the arraylist was past the end of the arraylist:
Index: 1, Size: 1 The max index for size of 1 is 0
Check your logic and computations to see why that happened.
- 10-01-2010, 11:19 PM #6
Ok I figured out why my probe wasnt printing because I had my incrementer before the print. Now my Problem is that I'm reading in my data as a string and its assigning the string read in into the arraylist, but that isnt what I need. I read it in as a string just to make sure it read, but I need objects of type country in the arraylist and I cant do that because the add parameter needs a string. How do I put objects into my arraylists?
Last edited by Metastar; 10-01-2010 at 11:25 PM.
- 10-01-2010, 11:32 PM #7
What is the "add parameter"? Are you talking about the add method?the add parameter needs a string
Why does it need a String? Do you get compile errors?
All of the ArrayLists in your code are defined to contain only Strings.
If you need to have different data types in an arraylist, you need to define one for that data type.
Please show your code, especially where the problems are.
- 10-01-2010, 11:53 PM #8
Similar Threads
-
Creating an ArrayList from an ArrayList
By Klahking in forum New To JavaReplies: 17Last Post: 09-09-2010, 03:34 PM -
ArrayList question
By spatel14 in forum New To JavaReplies: 4Last Post: 07-07-2010, 10:02 PM -
Beginner question about ArrayList
By kesi in forum New To JavaReplies: 3Last Post: 09-19-2009, 11:30 PM -
arraylist question
By lisa.lipsky in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-16-2009, 11:07 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks