Results 1 to 3 of 3
  1. #1
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default Need help copy array into new array

    I'm trying to write a program that creates 2 objects that are arrays and copies whats in one of them into the other. The program has an integer named how_many that counts the number of indexes that need to be copied into the array. When I run the program I get an array out of bounds exception and none of the indexes are copied over when I try to print it. I don't know where I'm making a mistake and I would really appreciate some help on this.

    thanks

    Java Code:
    public class Intcoll2 
    {
    	   private int[] c;
    	   private int how_many = 0;// this is what tells it how many indexes get copied over to the array
    
    	   public Intcoll2()
    	   {
    	      c = new int[500];
    	      c[0] = 0;
    	   }
    
    	   public Intcoll2(int i)
    	   {
    	      c = new int[i];
    	      c[0] = 0;
    	   }
    
    	   public void copy(Intcoll2 obj)//this is the method that copies the array and where I'm having trouble 
    	   {
    		   if (this != obj)
    		   {
    			   c = new int[obj.c.length]; // creates the array with the length of the other array
    			   int j = 0;
    			   while (obj.how_many > this.how_many) // I think this is wrong but I dont know whats wrong about it
    			   {
    				   c[j] = obj.c[j]; j++; // this is supposed to copy the array 
    				 
    			   }
    			 
    			   //c[j] = 0;
    			   
    		   }
    	   }
    
    	   public boolean belongs(int i)
    	   {
    		  int j = 0; 
    	      if(i > 0)
    	      {
    	      while ((j < how_many)&&(c[j] != i)) j++;
    	      }
    		return (j < how_many);
    	    
    	   }
    
    	   public void insert(int i)
    	   {
    	      if (i > 0)
    		  {  
    	    	  
    		     int j = 0;
    		     while ((j < how_many) && (c[j] != i)) j++; 
    		     if (j == how_many)
    		     {
    		    	   c[j] = i;
    		    	   how_many ++;
    		    
    		     }
    		  }
    			 
    	   }
    
    
    	   public void omit(int i)
    	   {
    	      int j = 0;
    	      while ((c[j] != 0)&&(c[j] != i)) j++;
    	      if (c[j] == i)
    	      {
    	         int k = j+1;
    	         while (c[k] != 0) k++;
    	         c[j] = c[k-1]; c[k-1]=0;
    	      }
    	   }
    
    	   public int get_howmany()
    	   {
    	      int j=0, howmany=0;
    
    	      while (c[j]!=0) {howmany++; j++;}
    	      return howmany;
    	   }
    
    	   public void print()
    	   {
    	      int j = 0;
    	      while (j < how_many)
    	      {
    	         System.out.println(c[j]);
    	         j++;
    	      }
    
    	   }
    
    	   public boolean equals(Intcoll2 obj)
    	   {
    	      int j = 0; boolean result = true;
    	      while ((c[j] != 0)&&result)
    	      {
    	         result = obj.belongs(c[j]); j++;
    	      }
    	      j = 0;
    	      while ((obj.c[j] != 0)&&result)
    	      {
    	         result = belongs(obj.c[j]); j++;
    	      }
    	      return result;
    	   }
    	   
    	   
    	public static final int SENTINEL = 0;
    	
    	public static void main(String[] args) 
    	{
    		Scanner kb = new Scanner(System.in); 
    		int value;
    		
    
    		Intcoll2 X = new Intcoll2();// whats in this array needs to get copied into object Y
    		Intcoll2 Y = new Intcoll2();
    		
    		System.out.println("Enter an integer to insert into collection X from Intcoll2 or 0 to quit: ");
    		value = kb.nextInt();
    		while(value != SENTINEL)// this fills the array X
    		{
    		     
    			X.insert(value);
    		    	
    		    System.out.println("Enter an integer to insert into collection X from Intcoll2 or 0 to quit: ");
    		    value = kb.nextInt();
    		}
    		System.out.println("The values in collection X are: ");
    		X.print();
    		
    		Y.copy(X); // this is supposed to copy X into Y
    		System.out.println("Collection Y is now a copy of Collection X and contains: ");
    		Y.print();
    		
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You've got a while loop whose condition, the statement that tells the while loop when to stop, has no chance of changing, of becoming false, so there should be no surprise that you're running into the problem that you're having. The only thing you're changing inside of the while loop is "j" so if it were to be a valid while loop, you'd check j in the condition, but even this shouldn't be done. You know how many times you should loop before the loop begins -- the length of the array -- and in this situation you shouldn't even use a while loop but a for loop since this is what they were explicitly made for. So get rid of the while loop and use a for loop. (or ArrayCopy or clone).

  3. #3
    Get_tanked is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    ok, so I changed the copy method to this but the array still doesn't print when I try to print it, which makes me think it didnt copy over any suggestions?
    Java Code:
    	   public void copy(Intcoll2 obj)
    	   {
    		   if (this != obj)
    		   {
    			   c = new int[obj.c.length];
    			   
    			   for(int j = 0; j < obj.how_many; j++)
    			   {
    				   c[j] = obj.c[j]; 
    			   }
    	   
    		   }
    	   }

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  4. Replies: 1
    Last Post: 05-20-2010, 10:17 PM
  5. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 AM

Posting Permissions

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