Solved

Slight modification from Array to ArrayList

Posted on 2003-11-05
11
443 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 20 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now