I have this code that searches one object in an array and removes it. I'm having a problem with its position, since some other methods work with this array (and it gives me a NullPointerException every time). My method looks like this:
public void deleteHotel(String hotelName) {
for (int i = 0; i < this.hoteis.length; i++) {
if (this.hoteis[i].getName().equalsIgnoreCase(nomeHotel)) { //searches the array, looking for the object that has the inputted name
this.hoteis[i] = null; //makes that object null
if (this.hoteis.length > 1 && this.hoteis[this.hoteis.length - 1] != null) { //for arrays with lenghts bigger than 1 (since there's no problem with an array with one position)
for (int x = i; x < this.hoteis.length; x++) {
this.hoteis[x] = this.hoteis[x + 1]; //makes that null position point to the next position that has an object, and then that position points to the object in the next position and so on
}
this.hoteis[this.hoteis.length - 1] = null; //since the last to positions will be the same, make that last one null
Hotel[] hoteisTemp = new Hotel[this.hoteis.length - 1];
for(int x = 0; x < this.hoteis.length - 1; x++){ //create a new array with one less position, and then copy the objects on the old array into the new array, then point the old array to the new array
hoteisTemp[x] = this.hoteis[x];
}
this.hoteis = hoteisTemp;
}
i = this.hoteis.length;
}
}
}
When I use other methods (for example, one that returns the implemented toString()s of each object) it gives me a NullPointerException. Can you guys identify the error in the code? Much appreciated...
I have tested your function and I see what you mean by it getting a nullpointerexception, this is due to the array not resizing the list - which is due to your conditional:
if (this.hoteis.length > 1 && this.hoteis[this.hoteis.length - 1] != null).
Simply removing this solved the issue, here is the working function:
public static void deleteHotel(String hotelName) {
for (int i = 0; i < hotels.length; i++) {
if (hotels[i].getName().equalsIgnoreCase(hotelName)) { //searches the array, looking for the object that has the inputted name
hotels[i] = null; //makes that object null
for (int x = i; x < hotels.length -1; x++)
hotels[x] = hotels[x + 1]; //makes that null position point to the next position that has an object, and then that position points to the object in the next position and so on
Hotel[] hoteisTemp = new Hotel[hotels.length - 1];
for(int x = 0; x < hotels.length - 1; x++) //create a new array with one less position, and then copy the objects on the old array into the new array, then point the old array to the new array
hoteisTemp[x] = hotels[x];
hotels = hoteisTemp;
break;
}
}
}
Though please consider using a list of some sort when needing to use a list with a changing size.
The fundamental problem is that you're not allowing for where you removed the entry from the array.
Instead of
for(int x = 0; x < this.hoteis.length - 1; x++){
you want
for(int x = 0; x < this.hoteisTemp.length; x++){
(although that's a style choice)
and more significantly, instead of
hoteisTemp[x] = this.hoteis[x];
you want
int y = x < i ? x : x + 1;
hoteisTemp[x] = this.hoteis[y];
You also want to get rid of everywhere you're setting array elements to null, because if your copying logic works correctly, that's unnecessary.
For this use case, I would consider using one of the List implementations.
Consider rewriting your code
List result = new ArrayList();
for (int i = 0; i < this.hoteis.length; i++) {
if (!this.hoteis[i].getName().equalsIgnoreCase(nomeHotel)) {
result.add(this.hoteis[i]);
}
}
return result.toArray();
The point where you're shifting the array elements towards the left
for (int x = i; x < this.hoteis.length; x++) {
this.hoteis[x] = this.hoteis[x + 1];
}
The loop condition should be x < this.hoteis.length - 1 because at the last iteration when x = this.hoteis.length - 1 the index value this.hoteis[x + 1] would throw a NullPointerException.
Try using ArrayList it will simplify your code complexity.Here is the link to documentation.
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
Related
This question already has answers here:
Removing an element from an Array (Java) [duplicate]
(15 answers)
Closed 4 years ago.
How to remove an object from an array without using ArrayList?
I try to create miniaplication using Swing.
At this moment it contains the main window, and circles in it, that going around, and when I click on the circle - it disappears.
And when circle disappears it should be removed from the array. I don't understand how to do it.
Here is the code:
Arrays with objects;
Sprite[] sprites = new Sprite[10];
Method to delete object:
private void deleteCircle(GameCanvas gameCanvas) {
gameCanvas.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
for (int i = 0; i <sprites.length ; i++) {
boolean takeDamage = x >= sprites[i].getLeft() && x<= sprites[i].getRight() && y >= sprites[i].getTop() && y <= sprites[i].getBottom();
if (takeDamage){
sprites[i].halfHeight = 0;
sprites[i].halfWidth = 0;
}
}
for (int i = 0; i <damageSprites.length ; i++) {
if (sprites[i].halfHeight == 0 && sprites[i].halfWidth == 0){
sprites = (Sprite[]) ArrayUtils.removeElement(sprites, i);
}
System.out.println(Arrays.toString(sprites));
}
}
});
}
if object.halfHeight = 0 and object.halfWidth = 0
it should be considered like it not exists, and should be removed from the array:
Try to remove it like this, but this doesn't work
for (int i = 0; i <damageSprites.length ; i++) {
if (sprites[i].halfHeight == 0 && sprites[i].halfWidth == 0){
sprites = (Sprite[]) ArrayUtils.removeElement(sprites, i);
How can I remove the object from Array without using ArrayList?
Like everyone else, I'd recommend a List for most use cases, but you've given reasons for not using one.
I'm not sure why ArrayUtils.removeElement isn't working for you, but since you're wanting to learn about arrays, I'll explain how this would be done without a helper method:
int indexToRemove = 4; //the index we're removing
Object[] newArray = new Object[array.length-1];
for(int i=0; i<array.length;i++){
if(i<indexToRemove){
newArray[i] = array[i];
}
else if(i>indexToRemove){
newArray[i-1] = array[i];
}
}
This loops through the original array, copying each item over to a new array. When it hits the deleted index, it skips a copy, then continues from the next index, adjusting the indices by -1 so that they match up correctly.
work like this:
for (int i = 0; i <sprites.length ; i++) {
if (sprites[i].halfHeight == 0 && sprites[i].halfWidth == 0){
sprites = (Sprite[]) ArrayUtils.remove(sprites, i);
}
just change:
ArrayUtils.removeElement(sprites, i);
to:
ArrayUtils.remove(sprites, i);
Using an ArrayList, I need to subdivide a deck into two sections, one top section, and one bottom section. The top section will be the front of the ArrayList arr. If the size of the ArrayList arr happens to be odd, the top section size must be one more than the bottom section. Below you will see a few more specifications, there seems to be a slight logic error, but I'm having trouble figuring out where. As you can see, I have pretty much all of the code written and I feel as though this should be working. I need to shuffle without using collections.
for(int i =0; i<topHalf.size();i++){
topHalf.size() will return 0 because you have no elements in it yet. When you initialize it you are just allocating a size for the underlying array but the arraylist will have a size of 0...
As an aside you could use the sublist method.
// divide by two and round up
int middle = (int)(arr.size() / 2.0f + 0.5f);
ArrayList<Battleable> topHalf = arr.sublist(0, middle);
ArrayList<Battleable> bottomHalf = arr.sublist(middle, arr.size());
The easiest way is to use the 'sublist' method. You can do:
Double middle = Math.ceil(new Double(arr.size())/2);<br>
topHalf = arr.subList(0, middle.intValue());<br>
bottomHalf = arr.subList(middle.intValue(), arr.size());
The only change I would have made is adding a ternary operator to (simplify?) the code a little bit:
ArrayList<Battleable> topHalf = new ArrayList<Battleable>();
int topSize = arr.size() % 2 == 0 ? arr.size()/2 : (arr.size()/2)+1;
for(int i = 0; i < topSize; i++) {
topHalf.add(i, arr.get(i));
}
ArrayList<Battleable> bottomHalf = new ArrayList<Battleable>();
int count = topHalf.size();
int bottomSize = arr.size() - topHalf.size();
for(int i = 0; i < bottomSize; i++) {
bottomHalf.add(i, arr.get(count));
count++;
}
int x = 0, y = 0;
int end = arr.size();
for(int i = 0; i < end; i++) {
if(I % 2 == 0) {
arr.add(i, topHalf.get(x));
x++;
} else {
arr.add(i, bottomHalf.get(y));
y++;
}
}
My program will be creating a two dimensional grid / ArrayList. It will search for all boxes around a specific box and return their elements. However if the box is at an edge it may not have any surrounding boxes in the grid. So we will try to access an empty slot in an arrayList, possibly slot -1.
Is there anyway I can write some code like this in Java:
ArrayList arr = new ArrayList();
//add 5 elements to arr
for(int i = 0; i<10; i++){
if(arr.get(i) is out of bounds){
System.out.println("No elements here");
else{
System.out.println(arr.get(i));
}
You can check to see if i is outside the array bounds.
if i >= arr.size();
Though a better solution would be to loop over the contents of the array with a for each loop like so:
for (Object i : arr){
// Do something
}
You need boundary control check. For instance, if you need to loop around the neighbors of a point x, y, you could do
// assuming that you're checking around point x and y
// here you set minI, maxI
int minI = Math.max(0, x - 1);
int maxI = Math.min(listMax - 1, x + 1);
for (int i = minI; i <= maxI; i++) {
// here you set minJ and maxJ
int minJ = Math.max(0, y - 1);
int maxJ = Math.min(innerListMax - 1, y + 1);
for (int j = minJ; j <= maxJ; j++) {
// do your stuff here
}
}
Please bear within as it might be difficult to understand
I have an array of jbuttons 50 size big, for this example ill use 7 I have jbutton object within 1 2 3 4 6 7 but not 5. These are printed on the screen. I want to remove these jbuttons however all buttons up to 5 are removed while the last two are not.
for(int i = 1; i < 51; i++){
if(seat.buttonArray[i] == null){
remove(seat.buttonArray[i]);
seat.buttonArray[i] = null;}
}
There is no way to remove element from array, assuming you want latter indexes changed after remove. For this purpose, you should use List:
Iterator buttonIterator = seat.buttonList.iterator();
while (buttonIterator.hasNext()) {
Object button = buttonIterator.next(); //or more specific type, if your list was generified
if (button == null) { //or some other criteria, wrote this just as an example
buttonIterator.remove();
}
}
If using array is mandatory, you have two options:
Set seat.buttonArray[i] to null value, indicating it has been removed;
Recreate array each time you deleted something. See System.arraycopy javadoc for details, although I do not recommend this approach because of performance considerations.
You could store the values in a temp array and then copy what you want back into your original array. Somewhat similar to this:
int myArray[50];
int temp[50];
int good;
for (int i = 0; i < 50; i++) {
myArray[i] = i;
}
for (int i = 0; i < 50; i++) {
temp[i] = myArray[i];
}
good = 0;
for (int i = 0; i < 50; i++) {
if (i < 10) {
} else {
myArray[good] = temp[i];
good += 1;
}
}
Looks messier than I first thought... But it essentially does what you're wanting.
I have the code below:
int lines = 0;
while(lines < 2)
{
int[] oldarr = parr;
for(int i = 0; i < arrsize; i++)
System.out.print(" " + oldarr[i]);
System.out.println();
for(int i = 0; i < arrsize; i++)
{
if(i == 0)
parr[i] = 0;
else
parr[i] = Math.abs(oldarr[i] - oldarr[i-1]);
}
lines++;
}
parr is an array of integers of size [arrsize]. Each time through this loop I want to print the value of each index in parr, then set each index to the difference between the index before it and itself. Currently it gives me the correct (hardcoded) originally parr. But the next(first) iteration of changing parr gives me unexpected values; they are not even close to the difference between the two neighboring values..
Any ideas?
You aren't copying your array with this line:
int[] oldarr = parr;
The two variables are still pointing at the same array.
To get a copy, you can do:
int[] oldarr = Arrays.copyOf(parr, parr.length);
In your second for loop, you are setting the new value to the difference of the current value and the previous value, but the previous value was already changed in the previous iteration of the for loop.
Change your second for loop iteration to iterate through the array backwards, so your calculations don't depend on previous calculations.
for(int i = arrsize - 1; i >= 0; i--)