java - Reference new object through void method -


i'm working on assignment deals array manipulations java. right have delete elements inside middle of array. know arrays, once they're created cannot changed in length. decided make new object, , have former reference point new array.

public class a{   public static void main(string[] args){     b test = new b(val);     test.cut(2,4);     test.display();   }  class b{       private obj[] array;    b(obj val){     construct   }    public void cut(int i, int j){      b newobject = new b(val);         ...      newobject.array = this.array;      newobject = this;   } } 

the issue when display test object, show me original test object contents rather newobject contents. since void method, can't return object. how reference new object then? last 2 lines cut method seem have no effect @ all. know arraylist preferable things this, being homework assignment forced use arrays.

now know arrays, once they're created cannot changed in length.

this true.

but reference pointing array inside b object instance can changed (as didn't declare final):

public void cut(int i, int j){     object[] newarray = new object[len];      //...  copying array content ...     this.array = newarray; } 

beware of thread safety issues such mutability causes. mutable objects frowned upon...


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -