array problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: array problem

  1. #1
    Join Date
    Sep 2006
    Posts
    23

    array problem

    ..
    Last edited by torrinment; 11-22-2006 at 12:49 AM.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What happens if you choose another element to delete - let's say, the one at element 4?

    how about code like this ... a bit of a turn around from yours but it looks more direct and clear to me:

    Code:
    for( i = index; i < array.length -1; ++i)
    {
        array[index] = array[ index + 1];
    }

  3. #3
    Join Date
    Sep 2006
    Posts
    23
    ..
    Last edited by torrinment; 11-22-2006 at 12:50 AM.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I recognize my mistake now, after I wrote a test:

    Code:
    import java.util.*;
    import java.io.*;
    
    public class QuickTest
    {
    	private int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    
    	public QuickTest()
    	{
    	}
    
    	public void deleteAndArrange( int index )
    	{
    		for( int i = index; i < myArray.length -1; ++i)
    		{
        		     myArray[ i ] = myArray[ i + 1];
    		}
    	}
    
    	public void printArray()
    	{
    		System.out.println(Arrays.toString( myArray ) );
    	}
    
    	public static void main (String[] args )
    	{
    		QuickTest myTest = new QuickTest();
    		myTest.printArray();
    		myTest.deleteAndArrange( 4 );
    		myTest.printArray();
    	}
    }
    and I realize that it needs to be array[i]=array[i+1], not index and index + 1 (which resulted in the same output as yours ...)
    Last edited by nspils; 11-21-2006 at 11:20 PM.

  5. #5
    Join Date
    Sep 2006
    Posts
    23
    thanks dude

  6. #6
    Join Date
    Sep 2006
    Posts
    23
    ..
    Last edited by torrinment; 11-22-2006 at 12:50 AM.

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You are dealing with one of the major drawbacks to using arrays - the difficulty in keeping a unified structure if you are deleting elements inside the array. If this is something you are going to do a lot of, use ArrayLists - same ease of accessing, for all intents and purposes just as fast as array access, but more flexible. If you really must use arrays, take a look at using the System.arraycopy method - I've used it, before, to move from one sized array to another, copying some or all of the elements of the first.

  8. #8
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Why!

    It is deleting the fifth not the fourth!

  9. #9
    Join Date
    Jul 2005
    Posts
    78
    Did you say array[4].delete() ? (or whatever your code is...)

    The element that is numbered "4" is actually the fifth element, as counting starts from zero. So the fourth element is array[3].

  10. #10
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    I am using Blue J

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    [1, 2, 3, 4, 6, 7, 8, 9, 0, 0]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    [1, 2, 3, 4, 6, 7, 8, 9, 0, 0]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    [1, 2, 3, 4, 6, 7, 8, 9, 0, 0]

    This is what I am getting.
    If I enter any number like 2 or 6, it would not go.

  11. #11
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Where are you entering a number like 2 or 6?

    If you want to use the code I posted and want to take user input, you can take it from the command line as an argument (would be args[0]) or you could ask for input from the console from the user. In both cases, you would need to assign a value to "index" based on the input and put that value as the argument for the statement:

    myTest.deleteAndArrange( index );

    perhaps by modifying method main something like this (the command line version):

    Code:
    public static void main (String[] args )
    	{
    		QuickTest myTest = new QuickTest();
    		myTest.printArray();
    		int index = Integer.parseInt( args[0] );
    		myTest.deleteAndArrange( index );
    		myTest.printArray();
    	}
    Last edited by nspils; 11-23-2006 at 10:42 AM.

  12. #12
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    Integer.parseInt
    How it works?
    I was unable to figure it out.

  13. #13
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The static method parseInt of the Integer class takes a String and returns the integer value of that String. Since input from the command line is stored as a String in the args String array - you need to convert the value in the command line argument to an int.

  14. #14
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    I am learning here mroe!

    Hi,
    I am learning here more than I have learned from my teacher!!!
    Now, I have noticed that every time it will reprint the Original array. I expect it to print the modified one. i. e. 1234578900 to start with instead 1234567890.

    We have to modify the Main method to do that.
    I"ll give it a try.
    Last edited by Kinda Electroni; 11-25-2006 at 09:58 AM.

  15. #15
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The code I posted was very limited in its functionality. It is a "one shot" deal. The array - which you have modified - has disappeared when the program output itself to the console and then finished execution.

    To keep the modified array around for deleting yet another element, you'll have to modify the program, switching to asking for input from the user, and having a menu of some sort which will cease execution when you input some value.

    What would you do to restructure the program?

Similar Threads

  1. Reliability Problem
    By elise in forum Java
    Replies: 0
    Last Post: 10-30-2002, 05:40 AM
  2. Changing Array Length Problem
    By Damo in forum ASP.NET
    Replies: 3
    Last Post: 10-15-2002, 03:58 PM
  3. Replies: 2
    Last Post: 09-02-2000, 08:04 PM
  4. SafeArrayCopy SLOWER than iterating string array!
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 10
    Last Post: 06-16-2000, 06:34 AM
  5. SafeArrayCopy SLOWER than iterating string array!
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 0
    Last Post: 06-12-2000, 09:15 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center