Hello,

I need to write a remove method for an array of 10 things.

So object can can a name. I have a getName() method and others that sort out things. The array can only have 10 objects inside. However when I use this method it doesn't remove anything. I'm not using an arraylist so I can't use those method which would make this way easier.

I start out with

public void remove(Object a){
for(int i = 0; i <= object.length; i++) {
if(this.object[i].getName().equals(a.getName())) {
this.object[i] = null; 
}
}
}

any ideas. I understand this is without some information so if you need something more to understand it just ask I will provide it as quick as possible.

maybe this could help. (ive assumed a sample scenario)

public class Orange {

    private String name;

    public void setName(String name){
        this.name = name;
    }
    
    public String getName(){
        return this.name;
    }

 public void remove(Orange o[], Orange o1){
        for(int i=0; i<o.length; i++){
            if(o[i].getName().equals(o1.getName())){
                o[i] = new Orange();
            }
        }
    }
}

and to check

public static void main(String a[]){
        Orange[] o = new Orange[2];
        for(int i=0; i<o.length; i++){
            o[i] = new Orange();
            o[i].setName("Orange_" + (i+1));
        }
        
        Orange o2 = new Orange();
        o2.setName("Orange_1");

        o2.remove(o, o2);
        for(int i=0; i<o.length; i++){
            System.out.println(o[i].getName());
        }
    }
Member Avatar for ztini

There is no remove method for arrays. That's the beauty of OOP, you can write your own! :)

Here's a simple example:

public class Array {

	private String[] array = new String[10];
	
	public Array() {		
		for (int i = 0; i < array.length; i++) {
			array[i] = 
				Character.toString((char) (i + 65)) + 
				Character.toString((char) (i + 66)) + 
				Character.toString((char) (i + 67));
		}
	}
	
	public void print() {
		for (String word : array) 
			System.out.print(word + " ");
		System.out.println();
	}
	
	public String removeByIndex(int index) {
		String removed = "";
		
		if (index >= 0 && index < array.length) {
			for (int i = index; i < array.length - 1; i++) {
				removed = array[i];
				array[i] = array[i + 1];
			}
			array[array.length - 1] = null;
		}
		
		return removed;
	}
	
	public String removeByValue(String value) {
		for (int i = 0; i < array.length; i++)
			if (array[i].equals(value))
				return removeByIndex(i);
		return null;		
	}
	
	public static void main(String[] args) {
		Array arr = new Array();
		
		arr.print();
		arr.removeByIndex(0);
		arr.removeByIndex(4);
		arr.removeByValue("BCD");
		arr.print();
	}
}

Console:
ABC BCD CDE DEF EFG FGH GHI HIJ IJK JKL
CDE DEF EFG GHI HIJ IJK JKL null null null

**Updated

1. Code you posted shouldn't be compiling as Object doesn't have getName().
2. "However when I use this method it doesn't remove anything": How do you check whether something is removed or not? E.g. if you expect that the size of the array would change then know that it won't.

Simplest would be if you can post the whole code with the output it produces.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.