0

I have a list of users who are playing a game. They have some statistics, which I am storing in arrayList newList. Once the game exits, I am storing the arrayList value in a .dat file. Now thing is I will need to update a record of a user, if he already exists in my .dat file. I thought of using 3 arrayList over here.

1. ArrayList newList will get the records from the file.
2. ArrayList oldList will then store the replica of newList.
3. Game ends. Compare arrayList newList and oldList, and store the updated list in ArrayList users.
4. Store the ArrayList users in a file.

void compare()
    {
        Player obj1=null,obj2=null;
        int newSize = newList.size();   //stores the new records of the users.
        int oldSize = oldList.size();   //stores the old records of the users.
        for(int i=0;i<oldSize;i++)
        {
            for(int j=0;j<newSize;j++)
            {
                obj1=newList.get(i);
                obj2=oldList.get(j);
                if(obj1==obj2)
                {
                    users.add(obj1);
                }
                else
                {
                    users.add(obj2);
                }
            }

        }
    }

//store the records of users in the filename.dat

Will this logic work?

MrCoder
  • 103
  • 1
  • 2
  • 10

1 Answers1

1

Using a plain file to store "session data" probably is not the best approach, you could find issues related with concurrency, I/O blocking, etc.

In you case, I'd use some embedded DB, like SQLite or H2, I've used H2 in a similar scenario and It works pretty well.

However, if you want (for any reason) to store the data in a file by yourself, then I suggest to use a Map instead of List, using the username as key or whatever other unique field to identify the user, so you can get if user exists or not easily.

On the other hand, your code doesn't make too much sense, maybe I'm not understanding exactly what is your point, I'd use a code similar to this:

  List<Player> users = new ArrayList<Player>(oldList);

  for(String newUser: newList)
  {
      if (!users.contains(newUser)) {
          users.add(newUser);
      }
  }

Previous code, get all oldUsers and add the new ones that are not included in the old list, ¿ is this what you need ?

Roberto
  • 8,586
  • 3
  • 42
  • 53
  • You are suggesting pretty advanced stuff over here. I am just trying to create a small console based program. I am just a beginner in Java. – MrCoder May 18 '14 at 09:34
  • Well, I think that the use of a `Map` (`HashMap`) instead a `List` is not so "advance", that should simplify your code to manage stored users. – Roberto May 18 '14 at 09:43
  • I am doing loads of operation through list. Changing it to Maps, will mean a lot of re-work for me. Could you please suggest what is wrong with above code? This does not seem to work. – MrCoder May 18 '14 at 09:47
  • Ok, but I don't understand what are you trying to store un `users`list, your code is a bit confusing. Could you explain it a bit ? – Roberto May 18 '14 at 09:54
  • @MrCoder, Please, review my updated answer, if it isn't what you need, please explain it a bit further, from a functional point of view – Roberto May 18 '14 at 10:01
  • I am trying to update a file. I thought of doing this through ArrayList, however this is becoming tedious than what I thought. I am now thinking of deleting the file, once I read the contents from the file into my ArrayList, and then keep on updating the contents by creating a new file. Will this be a good logic? – MrCoder May 19 '14 at 07:31
  • Of-course if the program stops working due to xyz exception, there is always a risk of losing the data, which I just deleted:-( – MrCoder May 19 '14 at 07:32
  • You can move/rename the file instead of delete it, then create the new one and if all goes Ok, then delete the renamed file. – Roberto May 19 '14 at 07:37