[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Slight modification from Array to ArrayList

Posted on 2003-11-05
11
Medium Priority
?
463 Views
Last Modified: 2010-03-31
Here are the 3 classes, user,pair and FileParser:

class User {

     int userId;          //Value to store id
     Pair[] pairArray;     //Value to store float

     public User(int userId, Pair[] pairArray) {
          this.userId = userId;
          this.pairArray = pairArray;
     }

     public int getUserId() {
          return userId;
     }

     public Pair[] getpairArray() {
          return pairArray;
     }
}

This class needs to be modified to use an ArrayList for storing the pair objects, instead of an array.

  class Pair {

        int id;          //Value to store id
        float ratings;     //Value to store float

        public Pair(int id, float ratings) {
             this.id = id;
             this.ratings = ratings;
        }

        public int getId() {
             return id;
        }

        public float getRatings() {
             return ratings;
        }
   }

The pair class is fine and doesnt need changing.

import java.io.*;
import java.util.*;

class FileParser {
     public static void main(String[] args) throws Exception{
          FileReader fr =
              new FileReader("H:/Projects/Recommender System Project/ums_dataset.txt");     // to open the file
          BufferedReader inFile = new BufferedReader(fr);     // to read the file
          String line;     // String variable used to hold each line of the file
          StringTokenizer tokenizer;     // string tokenizer to parse each file line
          int[] idArray = new int[1000000];     // User id array
          Pair[] pairArray = new Pair[1000000];// array of cd id, and ratings pairs
          int index = 0;     // Index that we increment each time through the loop
          int movieId;     // value to hold movie id
          float ratings;     // value to hold movie ratings
          while((line = inFile.readLine()) != null) {
               tokenizer = new StringTokenizer(line);     // tokenize the file line
               // now add the user id to the user id array
               idArray[index] = Integer.parseInt(tokenizer.nextToken());
               // initialize the movie id variable
               movieId = Integer.parseInt(tokenizer.nextToken());
               // initialize the cd ratings variable
               ratings = Float.parseFloat(tokenizer.nextToken());
               // now add those variables to the Pair array
               pairArray[index] = new Pair(movieId,ratings);
               // incrememnt index
               index++;

          }

     }
}

The FileParser class shouldn't have arrays of id's and pairs. Instead
it should have just one array (probably ArrayList) of Users.

Any help would be great.
0
Comment
Question by:override_y2k
  • 4
  • 4
  • 3
11 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 9686141
 You can simply do:

User [] users = new User[10000]; // 10000 users in an array

 while((line = inFile.readLine()) != null)
{
   users[index] = new User(Integer.parseInt(tokenizer.nextToken()), new Pair(movieId,ratings));
   index++;
}

  The String tokenization will remain the same.

  An ArrayList will be similar, you only have to use the methods defined in the ArrayList. Take a look at the ArrayList documentation: http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html
0
 
LVL 35

Expert Comment

by:girionis
ID: 9686150
 Actually you need to pass a Pair array in the constructor of User. If your Pair array is called:  pairArray just pass it along.
0
 
LVL 2

Author Comment

by:override_y2k
ID: 9686231
This isn't the solution i required.As i said i need to use arrayList instead of arrays.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:girionis
ID: 9686333
 Use arraylist.add(new User(Integer.parseInt(tokenizer.nextToken()),  pairArray[index]);
0
 
LVL 35

Expert Comment

by:girionis
ID: 9686335
 Wrong. Use:

Use arraylist.add(new User(Integer.parseInt(tokenizer.nextToken()),  pairArray);
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9686757
If you want to remove the references to arrays altogether, you could do this:

(Please check that it suits your needs, particularly the fact that the User class no longer stores an array of Pairs)

User.java
------------
class User {

     int userId;          //Value to store id
     Pair pairArray;     //Value to store float

     public User(int userId, Pair pairArray) {
          this.userId = userId;
          this.pairArray = pairArray;
     }

     public int getUserId() {
          return userId;
     }

     public Pair getpairArray() {
          return pairArray;
     }
}


FileParser.java
-------------------
import java.io.*;
import java.util.*;

class FileParser {
     public static void main(String[] args) throws Exception{
          FileReader fr =
              new FileReader("H:/Projects/Recommender System Project/ums_dataset.txt");     // to open the file
          BufferedReader inFile = new BufferedReader(fr);     // to read the file
          String line;     // String variable used to hold each line of the file
          StringTokenizer tokenizer;     // string tokenizer to parse each file line
//          int[] idArray = new int[1000000];     // User id array
//          Pair[] pairArray = new Pair[1000000];// array of cd id, and ratings pairs
          ArrayList userArrayList = new ArrayList();
          int id = 0;
          int index = 0;     // Index that we increment each time through the loop
          User user = null;
          int movieId;     // value to hold movie id
          float ratings;     // value to hold movie ratings
          Pair pair = null;
          while((line = inFile.readLine()) != null) {
               tokenizer = new StringTokenizer(line);     // tokenize the file line
               // now add the user id to the user id array
               id = Integer.parseInt(tokenizer.nextToken());
               // initialize the movie id variable
               movieId = Integer.parseInt(tokenizer.nextToken());
               // initialize the cd ratings variable
               ratings = Float.parseFloat(tokenizer.nextToken());
               // now add those variables to the Pair array
               pair = new Pair(movieId,ratings);
               user = new User(id, pair);
               userArrayList.add(user);
               // incrememnt index
               index++;

          }

     }
}
0
 
LVL 2

Author Comment

by:override_y2k
ID: 9688189
The user class needs to be modified to use an ArrayList for storing the pair objects, instead of an array.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9688221
OK.

Let us know if you have any problems.
0
 
LVL 15

Accepted Solution

by:
jimmack earned 80 total points
ID: 9688359
override_y2k,

When a questioner starts asking for all the last little details of a solution, the first thing I do is check their question history.  I saw that you have asked 15 questions, of which you haven't closed 8.  This is not good and is part of the reason why I posted what I did.

However, after a little research, I see that some of your previous questions have been left by the experts, not yourself.  I would recommend that you go through your previous questions and put a posting on the CS TA asking if they can refund your points.

A good example is http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20464667.html.

I can't see any reason why you can't reclaim these 500 points.  Some of the others may be similar.

Please clean up your question history.  If I hadn't done that extra bit to find out why you'd left so many open, I might not have bothered to come back to help you.

Since you don't seem to be leaving questions open deliberately, here is some more information:

User.java
------------
class User {

     int userId;          //Value to store id
     ArrayList pairArray;     //Value to store float

     public User(int userId, ArrayList pairArray) {
          this.userId = userId;
          this.pairArray = pairArray;
     }

     public int getUserId() {
          return userId;
     }

     public ArrayList getpairArray() {
          return pairArray;
     }
}


FileParser.java
-------------------
import java.io.*;
import java.util.*;

class FileParser {
     public static void main(String[] args) throws Exception{
          FileReader fr =
              new FileReader("H:/Projects/Recommender System Project/ums_dataset.txt");     // to open the file
          BufferedReader inFile = new BufferedReader(fr);     // to read the file
          String line;     // String variable used to hold each line of the file
          StringTokenizer tokenizer;     // string tokenizer to parse each file line
//          int[] idArray = new int[1000000];     // User id array
//          Pair[] pairArray = new Pair[1000000];// array of cd id, and ratings pairs
          ArrayList userArrayList = new ArrayList();
          int id = 0;
          int index = 0;     // Index that we increment each time through the loop
          User user = null;
          int movieId;     // value to hold movie id
          float ratings;     // value to hold movie ratings
          ArrayList pair = null;
          while((line = inFile.readLine()) != null) {
               tokenizer = new StringTokenizer(line);     // tokenize the file line
               // now add the user id to the user id array
               id = Integer.parseInt(tokenizer.nextToken());
               // initialize the movie id variable
               movieId = Integer.parseInt(tokenizer.nextToken());
               // initialize the cd ratings variable
               ratings = Float.parseFloat(tokenizer.nextToken());
               // now add those variables to the Pair array
               pair = new ArrayList();
               pair.add(new Pair(movieId,ratings));
               user = new User(id, pair);
               userArrayList.add(user);
               // incrememnt index
               index++;

          }

     }
}

Note that in this code, you only have information to add a single pair to a user, so the ArrayList only contains one object.
0
 
LVL 2

Author Comment

by:override_y2k
ID: 9694284
The reason all the other questions are open is i never got an answer that i used.Any solution which i used or used part of was awarded the points accordingly. What do you mean by "CS TA". I've never had points refunded, so i'm notquite sure how to do it.

Thanks again for you help.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9694363
No problem.

At the top of the page is a bar with all the top level "groups" (or TA's).  The rightmost one is CS (Customer Services).

You can post messages in there for 0 points.  Ask a question in there like:

"No one has successfully answered these questions, could I please have my points refunded?"

and include the links to all your open questions where no-one has managed to help ;-)

You should get a few hundred points back ;-)

Good luck.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question