Solved

Slight modification from Array to ArrayList

Posted on 2003-11-05
11
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

729 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