• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help in generating a second vector list from the frist vector which...  RSS feed

 
Zubi Pen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a generateList in a java class called examList.java that creates a list of all the exam taken by a particular student as a vector.

From the code below "items = studentExam.resultSet();" contains the vector list of students.




Heres my question: the "items" instance variable which is of type Vector holds the list of all exam details of a studentID. How can I take that list and create a another verctor were I can do some further calculations. I would give an example:

the generateList method give exam details of StudentID = 1. and when I use getMathsScore() (which belongs to the class examInfo, examList class extends examInfo) it gives me the maths score of StudentID = 1. Now I want to compare the score of this.getMathsScore() with last years score and only include the positive values in the second vector. So in my examInfo.java class I created this method.


Hence my question is once I generate the first vector list of exam results by, say for example "studentID = 1", taking that list how can I further process the list and then return only the positive values in the second vector list.

I am not very sure how to do this and would really appreciate any sort of guidance?

Thanks,
Zub
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you pass a List as an argument to the constructor of another List?
Then pass a Comparator to sort on student number with the Collections class method.
Then go through the two Lists with a for loop (not Iterator or for-each) and remove anybody where the before mark is less than the after mark.
 
zub kodi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I am quite a newbie in Java I am not quite sure with your method.
Inorder to frame my question more appropriately, below is how I am using the data in jsp, after generating the list.

and I use the hasMoreRows() method to loop through the records and display info, the code is:

So far the code works fine, but my question is with this line "<%=student.getCompareValue(lastYear)%>" Rather then
generating the list and looping throught the records with hasMoreItems() method and then getting the value by this method "<%=student.getCompareValue(lastYear)%>", I need advise on how can I simply add this value in the generateList() in the java code rather then getting the value in jsp, so I am trying to acheive something like this:

Have a new generateListObject() that takes in a object, here is the code:


Hence in the resultSet() (items = studentExam.resultSet() , I want to add the value from this method "studentExam.getCompareValue(lastYear)".

Thanks,

[ March 17, 2008: Message edited by: zub kodi ]
[ March 17, 2008: Message edited by: zub kodi ]
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by zub kodi:
Since I am quite a newbie in Java I am not quite sure with your method.
. . . but you posted on intermediate, so I presumed you did know.

You say you have a List; do you have a List with last year's results in? Having the present year's results and last year's results in separate objects without a link between them would make your task very difficult.

You can create a new List which is a copy of an old List easilySorting the List can be done with a Comparator; you get the static sort method from the Collections class:As an alternative, you can go through your new List (with a for loop, not for-each or Iterator) and use the List.remove() method if s.getMark() <= lastYear.getMark().

The hard part is putting the Student and last year's mark together.
 
zub kodi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell,

Thanks for your reply, really appreciate it. With regards to the last year's result,its not a list, its just object of "One Row". I will explain with an example, say I have two objects currentResult, and previousResult.
When I generate the list currentResult.generateList(), and loop through it, it gives me the following out:

studentID MathsResult
1 30
1 40
1 40


(You might be thinking why does studentID 1 have three maths result, well its just how the system works, students can have unlimited resits)

The above list is generated using a vector in the generateList() method

Now Object two which is "previousResult" has only "ONE RECORD", so for example the following record:
studentID preMathsResult
1 50


I want to use the Second Object as a parameter to pass in the generateList() method, so generateList(student.lastYearDetails previousResult) and add to the vector list.

So if I have the following code in jsp: currentResult.generateList(previousResult) it gives me the following list:

studentID MathsResult CompareValue
1 30 20 this is previous result - current result
1 40 10 this is previous result - current result
1 40 10 this is previous result - current result

Hence to clearify, the previousResult object only has one Record. I hope I didn't get you confused? I am trying to compute the CompareValue and add it to the vector in the generateList() method.

Thanks,
Zub
[ March 18, 2008: Message edited by: zub kodi ]
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid I don't quite understand how you are getting your students into a List. Maybe when I am more awake I shall?

I did however make a mistake in advice I gave you earlier, telling you not to use an Iterator; you can in fact use an Iterator and remove objects with its remove() method. Always declare the Iterator as a local variable and make sure nothing else can add or remove anything while the Iterator is in operation.
 
Zubi Pen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for your help so far. Actually its my fault, I never stated that I get the list from a database. I have a table called examDetails with fields studentID, mathsResult. The generatelist() method gets the list from tbl called examDetails by studentID.

And I have another table called PreviousExamResult that has one unique record for each studentID.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . in which case it might be easier to do the arithmetic before putting the Student objects into the List. BTW: Use ArrayList rather than Vector unless you specifically need synchronisation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!