?
Solved

Slight modification from Array to ArrayList

Posted on 2003-11-05
11
Medium Priority
?
456 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

800 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