Results 1 to 8 of 8
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default 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 code
    Last edited by Metastar; 10-01-2010 at 10:39 PM.

  2. #2
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    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 Main
    Java 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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    said incompatible types
    If you got errors compiling your code, Please copy and paste the full text here.

  4. #4
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    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.

  6. #6
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    the add parameter needs a string
    What is the "add parameter"? Are you talking about the add method?
    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.

  8. #8
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Oh I think I figured it out, I didnt realize the < > was the type it was looking for. Changed all to ArrayList<Country> and now the .add() takes the correct type, and yea I was reffering to the add method before. Thanks for showing me that my types were off :) got me out of a jam again Norm

Similar Threads

  1. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 03:34 PM
  2. ArrayList question
    By spatel14 in forum New To Java
    Replies: 4
    Last Post: 07-07-2010, 10:02 PM
  3. Beginner question about ArrayList
    By kesi in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 11:30 PM
  4. arraylist question
    By lisa.lipsky in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-16-2009, 11:07 AM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •