Indexing an ArrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Doegon
    New Member
    • Feb 2008
    • 14

    Indexing an ArrayList

    Hi guys how does one index an object with a collection in an ArrayList,the code i used reports an error."expected is an array and found is an array list" and the possible loss of precision error.this is the code

    while(inFile.ha sNext())
    {
    for(Worker worka : badiri){ //fills up the collection with worker objects
    badiri.add(new Worker(workerNa me,salaryRate)) ;
    }
    badiri.sort(); //sorts the collection


    for(int j= 0; j< badiri.length;j ++){ //computes the sum of the salary rates all the workers
    sum+= worka.getSalary Rate();
    }

    median = sum/badiri.length; //computes the median salary rate

    //output

    System.out.prin tln("Smallest Salary Rate= " + badiri[0].toString());
    System.out.prin tln("Median Salary Rate= " + badiri[median].toString());
    System.out.prin tln("Largest Salary Rate= " + badiri[badiri.length-1].toString());

    }//close while loop #2
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    At which lines do you get the errors? The output lines would give you an error, since unlike C++ vectors, ArrayLists do not use the [] to access members. You use .get() instead.

    Comment

    • Doegon
      New Member
      • Feb 2008
      • 14

      #3
      Thanks a lot man,the get() method worked but i still get an error on sorting the ArrayList.
      i used the method call of
      Collections.sor t(variablename) ;
      it doesn't seem to recognize the function.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        You have to import java.util.Colle ctions to use that. You will get a warning that it's an unsafe call for reason's I'm not entirely sure of...You /should/ be able to ignore that.

        Comment

        • Doegon
          New Member
          • Feb 2008
          • 14

          #5
          i have imported the java.util.*; class and it seems like no effect is shown the erro is still there.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Doegon
            i have imported the java.util.*; class and it seems like no effect is shown the erro is still there.
            If you want accurate help be accurate yourself: please show us the exact error
            diagnostic message for complete information. (that includes the line number where
            the error was found) and a bit of relevant code. The Collections class exists since
            Java 1.2 so it's definitely there including its methods.

            kind regards,

            Jos

            Comment

            • Doegon
              New Member
              • Feb 2008
              • 14

              #7
              here is the code and below is the error report i hpoe it helps


              ArrayList<Worke r> badiri = new ArrayList<Worke r>(); //create a collection of the workers

              double sum = 0.0;

              while(inFile.ha sNext())
              {
              for(Worker worka : badiri){ //fills up the collection with worker objects
              badiri.add(new Worker(workerNa me,salaryRate)) ;
              }
              Collections.sor t(badiri); //sorts the collection


              for(Worker worky : badiri){ //computes the sum of the salary rates all the workers
              sum+= worky.getSalary Rate();
              }

              for(Worker mmereki : badiri){
              if(sum/badiri.size() == mmereki.getSala ryRate()){ //prints the worker with median salary rate
              System.out.prin tln(mmereki.toS tring());
              }
              }

              //output

              System.out.prin tln("Smallest Salary Rate= " + badiri.get(0).t oString());
              // System.out.prin tln("Median Salary Rate= " + badiri.get(medi an).toString()) ;
              System.out.prin tln("Largest Salary Rate= " + badiri.get(badi ri.size()).toSt ring());

              }//close while loop #2

              inFile.close();
              outFile.close() ;
              }



              WorkerTester.ja va:10: cannot find symbol
              symbol : method sort(java.util. ArrayList<Worke r>)
              location: class java.util.Colle ctions
              Collections.sor t(badiri); //sorts the collection

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Doegon
                WorkerTester.ja va:10: cannot find symbol
                symbol : method sort(java.util. ArrayList<Worke r>)
                location: class java.util.Colle ctions
                Collections.sor t(badiri); //sorts the collection
                Read the API docs for that method: it needs something that is a Comparable,
                i.e. it can't sort just any type of object. Your Worker class doesn't implement that
                Comparable interface.

                kind regards,

                Jos

                Comment

                Working...