-
array problem
Last edited by torrinment; 11-21-2006 at 11:49 PM.
-
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];
}
-
Last edited by torrinment; 11-21-2006 at 11:50 PM.
-
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 10:20 PM.
-
-
Last edited by torrinment; 11-21-2006 at 11:50 PM.
-
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.
-
Why!
It is deleting the fifth not the fourth!
-
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].
-
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.
-
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 09:42 AM.
-
Integer.parseInt
How it works?
I was unable to figure it out.
-
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.
-
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 08:58 AM.
-
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
-
Replies: 0
Last Post: 10-30-2002, 04:40 AM
-
Replies: 3
Last Post: 10-15-2002, 02:58 PM
-
By Jack in forum VB Classic
Replies: 2
Last Post: 09-02-2000, 07:04 PM
-
By Mark Alexander Bertenshaw in forum VB Classic
Replies: 10
Last Post: 06-16-2000, 05:34 AM
-
By Mark Alexander Bertenshaw in forum VB Classic
Replies: 0
Last Post: 06-12-2000, 08: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks