Solved

Slight modification from Array to ArrayList

Posted on 2003-11-05
11
447 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
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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 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