Results 1 to 10 of 10
  1. #1
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default How do I empty my array to write new information to it?

    my array is an attribute of the class Movielist and contains objects from the class Movie; moviedatabase is an instance of movielist

    I have an "open" function and I want this to empty my array and then write new information imported from a file

    I know how to import information to add to my array, but in this case I must empty my array first... how do I do that? if I set moviedatabase to null then I can't add to it, and if I try instantiating it again immediately after it says it's a duplicate and won't let me - what do I do?

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Use an ArrayList instead. It has a clear() method and it is extendible, meaning you can add an 'unlimited' number of objects to it while an array is bound to the size on creation.
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Unfortunately I'm bound to this data structure, does anyone have any clever suggestions of how to do something like this with an array?

  4. #4
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Oh, well, if that´s the case...

    You could skip clearing the content of your array. Just put new objects in it from the start. The garbage collector will free your useless objects for you.

    Remember to keep track of the last object in the array otherwise you don´t know where your new objects end and the old objects start.
    I'm new to Java but I like to help where ever I can. :)

  5. #5
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    I did ponder that for a while... but I don't know how to deal with the problem of remaining values that were not overwritten

    I wish I could just .removeall or something, you know?

  6. #6
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Did you write class Movielist or do you have the code available? If so, post it here. That makes it much easier to discuss.
    I'm new to Java but I like to help where ever I can. :)

  7. #7
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    I have my entire code which includes other functions and is very messy... heres the method

    Java Code:
    		private void open(MovieList movieDatabase){
    			
    			String year;
    			String title;
    	        String row;
    	        String[] values = new String[4];
    	        
    			JFileChooser cf = new JFileChooser();
    			int result = cf.showOpenDialog(null);
    			File file = null;
    			if(result == JFileChooser.APPROVE_OPTION)
    				file = cf.getSelectedFile();
    	        
    	        try {
    	            BufferedReader in = new BufferedReader(new FileReader(file));
    	            while ((row = in.readLine()) != null){
    	                values = row.split(",");
    	                
                    //System.out.println("Values: "+values[0]+","+values[1]+","+values[2]);
                    
                    title = values[0];
                    	                int year_beg = title.indexOf("(")+1;
                    	                int year_end = title.indexOf(")", year_beg);
                    	                year = title.substring(year_beg, year_end);
                    
                    //System.out.println("Values: "+values[0]+","+values[1]+","+values[2]+","+year+"-----"+year_beg+"------"+year_end);
                            
                    Movie importedMovies = new Movie(values[0], values[1], values[2], year);           
            		movieDatabase.add(importedMovies);      
    	            }
    	      
    	        	} catch (FileNotFoundException e) {
    		            // TODO Auto-generated catch block
    		            e.printStackTrace();
    		        } catch (IOException e) {
    		            // TODO Auto-generated catch block
    		            e.printStackTrace();
    		        }
    			
    		}
    I havent tested the browse file part, I had it hardcoded to read from a specific file in the project directory before... so I don't know if that works

    the add function just puts the index to the end of the array and adds them, but you can see what I'm trying to do, it's just a matter of clearing the array

  8. #8
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    I'm confused now. What array do you want to clear? Your own values array or the internal array of movieDatabase? If you mean the latter, then this piece of code is not very helpful. In that case you should provide the code of MovieList.
    I'm new to Java but I like to help where ever I can. :)

  9. #9
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Yes I want to clear the internal array (myFravoriteMovies) attribute in instance movieDatabase of the MovieList class

    I wondered why you wanted code, I'm not sure how anything I've done could help with this. I don't want to post my entire program (basically the movielist class) because it's full of redundant code, but these are the attributes

    Java Code:
    	
    private Movie[] myFavouriteMovies;                
    private final int MAX_NUMBER_OF_MOVIES = 3000;       
    private int numberOfMovies;                        
    private static int index;

  10. #10
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Well, I would like to see the code so I, or someone else on this forum, can help you. That is the basic idea of this forum: help one another out.

    So, the Movielist is a class you wrote yourself. I do not know why you would want to stick to the array then. If you would replace it with an ArrayList you could store more than 3000 movies.

    That aside, I'd suggest to make a clear() method in Movielist. This method does nothing more than resetting numberOfMovies. Then, when add(Movie) is called, that movie will be stored in myFavouriteMovies[numberOfMovies] and numberOfMovies gets increased.

    Java Code:
        public clear() {
            numberOfMovies = 0;
        }
    
        public add(Movie m) {
            myFavouriteMovies[numberOfMovies++] = m;
        }
    Cheers,
    Erik
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. How to find out if part of an array is empty
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 10-13-2010, 06:09 PM
  2. How do I count empty spaces in a byte array?
    By nessa203 in forum New To Java
    Replies: 13
    Last Post: 01-11-2010, 05:46 PM
  3. Write empty Jtable columns to disk
    By ocean in forum New To Java
    Replies: 9
    Last Post: 12-03-2009, 03:02 PM
  4. How to write text file into Array
    By venkat.ravala in forum New To Java
    Replies: 13
    Last Post: 11-19-2009, 04:59 PM
  5. Replies: 3
    Last Post: 04-07-2008, 06:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •