permaculture playing cards*
The moose likes Beginning Java and the fly likes delete from an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "delete from an array" Watch "delete from an array" New topic
Author

delete from an array

Lesa
Greenhorn

Joined: Feb 27, 2000
Posts: 4
Could someone please tell me what I'm doing wrong? I have inserted numbers into an array and I have inserted another number into the array but I cannot delete a number from the same array. Any help would be greatly appreciated! The code is as follows:


[This message has been edited by Frank Carver (edited February 28, 2000).]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20660
    ∞

I think there must be some code missing cuz your for loops are not complete.
If you want to be deleting stuff from the middle, I suggest you look into using a vector. It makes life much easier.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Lesa
Greenhorn

Joined: Feb 27, 2000
Posts: 4
Thank you...I will do that!
Originally posted by Paul Wheaton:
I think there must be some code missing cuz your for loops are not complete.
If you want to be deleting stuff from the middle, I suggest you look into using a vector. It makes life much easier.

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
As far as the "missing code" is concerned - the problem is that your for loops include the "<" symbol followed immediately by alphanumerics - our browsers are interpreting that as an attempt as an HTML tag. Even though it turns out not to be an actual HTML tag, the browsers still don't display the "failed tag" correctly. To fix this, make sure that "<" or "<=" is followed immediately with a space, and then we'll all be able to see it.
Note also that the author of a post has the ability to go back and redit it later, using the paper-and-pencil icon at the top of the post you wish to edit. So Lesa, you can actually fix the problem yourself in the existing post by just inserting a few spaces. Or Paul can for that matter, since he's sheriff. Unfortunately I tend bar down the street, not here, so I don't have the power to make the fix for you.

"I'm not back." - Bill Harding, Twister
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I've tidied it up; I hope it now shows as intended. If it's still wrong, you'll have to edit it yourself, Lesa.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Thanks, Frank. Lesa- unfortunately I still can't really tell what this is supposed to do. The second for loop is going to throw an <code>ArrayIndexOutOfBoundsException</code>, since <code>int counterd2 = numblength</code> starts on the first position after the end of the array. And you should probably put in explicit curly braces { } for each loop and each if statement, just to be safe.
As for "deleting" an array element (which I don't see where you're trying to do this in the code you provide) - you can't. You can put another value, such as 0, in place of a value you no longer need - but if you really want to delete an element, you should probably take Paul's suggestion and use Vector instead of an array - or use the newer ArrayList instead.
Lesa
Greenhorn

Joined: Feb 27, 2000
Posts: 4
Let me start by saying thank you to all that are trying to help me. This may help you understand what I am trying to do. I have a program that I am supposed to write that accepts user input and creates an array of integers. I need to insert a number into the array that is gotten from input from the user and delete a number from the array that is gotten from input also. I am only putting everything into the main part of the program to get the methods to work then I am going to try to break the program up into classes. I did check into Vectors but we are not allowed to use them. This is the program that I have so far.

Note. Please put code or pre tags round your code examples (see the UBB Code link below the submission form for details). Without them, example code is almost impossible to read
[This message has been edited by Frank Carver (edited February 29, 2000).]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Gosh, there's a lot there. I'll tackle this piece by piece.
1. I did check into Vectors but we are not allowed to use them. I'm always disappointed at this sort of assignment. It's frustrating and pointless. Whoever wrote the course has little understanding of what Java development is about, and teaching students to ignore standard API classes is very poor practice.
2. I am only putting everything into the main part of the program to get the methods to work then I am going to try to break the program up into classes. This is rarely a useful way to work. What you've done is put so much in one place that it has become over complex and confusing. I don't believe that the answer is to split this problem into classes either - Java is an Object-Oriented language, but that doesn't mean you have to use lots of classes for everything. In this case, splitting it into several methods would be a much better choice.
I don't know what you have been taught on your course, but the most important thing about solving a problem like this is to understand what you are doing. Actually writing it in Java (or C++, or Lisp, or Basic ....) is only a small part of the solution. First, think how you would solve this sort of problem in the real world. Write a sequence of numbers on a piece of paper, and think what you would do to delete one. Try it with lots of different cases, to make sure you really understand it (what happens if the number to delete is not there? what happens if it is the first one in the array? what happens if it is the last one in the array? what happens if it appears in the array more than once? and so on.)
Only once you understand the problem, should you start designing some code to solve it.
3. Programming is about attention to detail. It's hard to get to grips with sometimes, but there is a very strong connection between tidy, readable code in a consistent style, and how well it works. Even if it doesn't work, it makes finding the bugs easier! So find a good style and stick to it. The "house style" for this site is a reasonable choice, and can be found at
http://www.javaranch.com/style.jsp.
Think about names for methods and variables, and try really hard to split your processing into digestible chunks. At a guess, I don't think any method in this example should be longer than ten lines or so, and many should be less. You are already heading in the right direction with your "get..." methods.
4. Test your assumptions. I don't see any test code in your example, but I would strongly recommend that you write a little bit of program to test each of your methods as you write them. Make sure each bit works completely before you move onto the next bit. That way you won't be tripped up by something that you are "sure" works.
The bottom line is that this is only a complicated assignment if you try and bite off too much at once. Understand the problem regardless of Java. Understand good program layout and style regardless of this particular problem. Split your design into small bits and solve one at a time.
For background reading, take a look at my "golden rules" at http://www.efsol.com/.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: delete from an array