java - Replacing the values in the original array to the new one -


i have got interface -

//interface - public interface intsequence {     int length();     int get(int index);     void set(int index, int value);     /**      * returns contiguous subsequence of size "size" starts      * index "index" , backed sequence;      * is, changing through {@link intsequence#set(int, int)}      * affects original sequence well.      * @param index starting position of subsequence      * @param size subsequence size      * @return sequence of ints      */     intsequence subsequence(int index, int size); } 

and class implements -

public class intarray implements intsequence {      int[] a;     static int test;     static int[] b;     static int[] c;     int[] d;     int use;     int j;     int[] mama;     int[] mama2;     int indexgeter;      public intarray(int size) {         j = size;         = new int[size];         b = new int[size];         = b;     }      public intarray(int index, int size, int[] array) {         this.a = array;         int counter = 0;          while(counter < size) {             array[counter] = array[index];             counter++;             index++;         }     }      @override     public int length() {         return a.length;     }      @override     public int get(int index) {         return a[index];     }      @override     public void set(int index, int value) {         a[index] = value;     }      @override     public intsequence subsequence(int index, int size) {         intsequence resultseq = new intarray(index, size, a);         return resultseq;     }     public static void main(string[] args) {         intsequence = new intarray(5);         a.set(0, 0);         a.set(1, 10);         a.set(2, 20);         a.set(3, 30);         a.set(4, 40);          system.out.println("initial array");         system.out.println("size: " + a.length());         (int = 0; < 5; ++i)             system.out.println("a[" + + "]: " + a.get(i));          system.out.println("creating subarray (2, 2)");         intsequence s = a.subsequence(2, 2);         system.out.println("s.size: " + s.length());          system.out.println("multiplying subarray's last element");         s.set(1, s.get(1) * 10);         system.out.println("subarray after modification:");         (int = 0; < s.length(); ++i)             system.out.println("s[" + + "]: " + s.get(i));          system.out.println("array after modification:");         (int = 0; < 5; ++i)             system.out.println("a[" + + "]: " + a.get(i));          a.subsequence(0, 1).subsequence(0, 1).subsequence(0, 1).set(0, -10);         system.out.println("first element changed to: " + a.get(0));     } } 

problem - here, want return subarray created using method intsequence subsequence(int index, int size). however, code doing when run return following output -

initial array size: 5 a[0]: 0 a[1]: 10 a[2]: 20 a[3]: 30 a[4]: 40 creating subarray (2, 2) s.size: 5 multiplying subarray's last element subarray after modification: s[0]: 20 s[1]: 300 s[2]: 20 s[3]: 30 s[4]: 40 array after modification: a[0]: 20 a[1]: 300 a[2]: 20 a[3]: 30 a[4]: 40 first element changed to: -10 

whereas, expected output -

initial array size: 5 a[0]: 0 a[1]: 10 a[2]: 20 a[3]: 30 a[4]: 40 creating subarray (2, 2) s.size: 2 multiplying subarray's last element subarray after modification: s[0]: 20 s[1]: 300 array after modification: a[0]: 0 a[1]: 10 a[2]: 20 a[3]: 300 a[4]: 40 first element changed to: -10 

issue - can see expected output above, when sub-array, values of original array (a) @ index - index should updated aswell value cast sub-array.

for example -

int[] = new int[] {1,2,3,4,5}; //we have original array on here //you use subsequence on a.subsequence(2,2); //now elements have s[0] = a[2]; //which 3 in case s[1] = a[3]; //which 4 in case  //you make changes on s[0] s[0] * 10; s[1] * 100;  //the original array indexes s referred should modified aswell cause did changes subarray //the new array a[0] = 1; a[1] = 2; a[2] = 30; a[3] = 400; a[4] = 5; 

i know worst way of making code gives expected output not allowed make method straight away have integer array return type in order achieve purpose. have been trying figure out way needful days now.

thanks lot taking time @ it!

in order implement

affects original sequence well.

part challenging you'd need wrap integers objects , store array or list of wrapper objects in implementation. way changing internal int in wrappers affect sequences since storing wrappers store references wrapper objects , not objects in case of int.

how works:

this reference object vs primitive types in java. in case of primitive types = b create copy of b in a, in case of objects = b copies reference object b a. therefore changing b's internals affect you'll see though reference a.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -