logo
down
shadow

Can't copy my object and change values


Can't copy my object and change values

By : user2955823
Date : November 22 2020, 10:48 AM
I wish did fix the issue. I'm having problems making a copy of an object to use and change values for that copy, instead it changes the values for both of my objects. Code for the object.
code :
public Board(int[][] layout){
  board = layout;
}
public Board(int[][] layout){
  this();
  for(int i=0; i<layout.length;i++)
   for(int j=0; j<layout[0].length;j++)
      board[i][j] = layout[i][j];
}


Share : facebook icon twitter icon
How to copy superclass object values to subclass object values?

How to copy superclass object values to subclass object values?


By : spacex
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , If performance is not an issue here, you can copy all the properties from one class to the other making use of reflection.
Check this link to this other question that explains how to do it:
Copy constructor that copy other object values

Copy constructor that copy other object values


By : excelnewbie
Date : March 29 2020, 07:55 AM
like below fixes the issue The only problem I see is with the reference to the Point class (I'm assuming that we are taking about java.awt.Point). This class is mutable, so the City class you are copying from can change it, and the change will be reflected in your copy also. Use the following code to copy the Point object:
code :
_cityCenter = new Point(other._cityCenter);
_centralStation= new Point(other._centralStation);
How to copy Java object without a change to the original affecting the copy

How to copy Java object without a change to the original affecting the copy


By : johntrollston
Date : March 29 2020, 07:55 AM
With these it helps You have declared value as static in the Node class.
If the attribute is static then it is shared by all instances of Node.
code :
private static int value;
private int value;
static class Node {

    private static int nbOfNode = 0;
    private int value;

    Node(int v) {
        nbOfNode++;
        value = v;
    }

    int getValue() {
        return value;
    }

    static int getNbOfNode() {
        return nbOfNode;
    }
}
Node n1 = new Node(11);
System.out.println(n1.getValue());
System.out.println(Node.getNbOfNode());
Node n2 = new Node(22);
System.out.println(n2.getValue());
System.out.println(Node.getNbOfNode());
copy from one object, change keys but keep the same values

copy from one object, change keys but keep the same values


By : FiberCode
Date : March 29 2020, 07:55 AM
Hope that helps Problem with your code is returning string whereas you need a object, i.e
code :
return { name:xxx.namezz , data: [xxx.price] }
let obj = { "items": [{ "namezz": "Bike", "price": 100 },{ "namezz": "TV", "price": 700 },{ "namezz": "Album", "price": 10 },{ "namezz": "Book", "price": 5 },{ "namezz": "Phone", "price": 500 },{ "namezz": "Computer", "price": 1000 },{ "namezz": "Keyboard", "price": 25 }]}

let final = obj.items.map(({ price, namezz }) => ({ namezz, data: [price] }))

console.log(final)
How to copy the data of one object to another object and perform subtraction of 2 object's values in javascript?

How to copy the data of one object to another object and perform subtraction of 2 object's values in javascript?


By : FREDDIE
Date : March 29 2020, 07:55 AM
wish of those help You should be able to use Array.reduce, Object.entries etc. to create the new, merged object from the "parent" objects.
code :
obj_1 = {
       "_id": "SPool",
       "w1": 60,
       "w2": 150
     }

obj_2 = {
        "_id": "SPool",
        "w1": 30,
        "w2": 120
      }
      
 function mergeObjects(obj_1, obj_2, propertyName) {
     let merged = { ...obj_1 };
     merged[propertyName] = obj_2;
     merged.diff = Object.entries(obj_1).reduce((acc, [key, value]) => { 
         if (obj_2[key] && typeof obj_2[key] === "number") { 
             acc[key] = value - obj_2[key];
         }
         return acc;
     }, {});
     return merged;
 }
 

 console.log("Merged objects:", mergeObjects(obj_1, obj_2, "obj_2"));
Related Posts Related Posts :
  • Why onNext has no effect
  • IntelliJ Idea 2018.2 no option for importing gradle project
  • Remove a negative element from an array w/o array.copy
  • Could this prevent overflow in Java?
  • In Java Swing, can I receive Caret events in real time?
  • I need help not using brute force when using JFrames and DrawWindows
  • How do I refactor ArrayList<MyClass> into MyClassLIst?
  • My program keeps saying that the method cannot be resolved
  • BubbleSort -my code returns random addresses
  • JavaFX components inside HTML?
  • Character in Java game not responding
  • Working around access denied in a FileWalking Tree in Java7
  • How to avoid if/else when using multple suppliers?
  • How to pass object from table cell editor to Table Model?
  • Paypal Sandbox payment state pending
  • sejda-console.bat passing parameters with spaces
  • Multiple Consumers Spring Kafka
  • Bitbucket pipeline: environmental variables in build.gradle file not recognized
  • Java static enum method to return a default enum value
  • What is the complexity of empty for loop?
  • Group randomly List of Lists as N subsets with K elements in each
  • How to set texture on a shape drawn by glDrawArrays()?
  • How to make a jlabel resizable according to the window
  • Java Mathematical Expression Syntax
  • assigning values to characters in java
  • Method overriding and Inheritance in java
  • given a set of lists/groups with a series of number, find matching numbers
  • Connection to HID USB device (keyboard and mouse) in android
  • Inject HttpServletRequest in CDI SessionScoped bean
  • Pool game in java - ball collision algorithm
  • Focus ScrollView to selected position programmatically - Android
  • Is it hibernate bug?
  • Glib memory allocation error
  • Android Proguard - step by step
  • Setting the pivot point of a JPanel to its center
  • java check time is greater time
  • how to save a screenshot (matlab)
  • Anti-aliasing filled shapes in libgdx
  • Gwt custom text box having baloon popup
  • How to change date dynamically for each element in an Array
  • JPanel won't add the JLabel text?
  • how to access the .properties file in ant if it is in different location
  • Reduce application memory footprint
  • java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session
  • What is the exact purpose of calling System.exit() in java
  • How to do you get output from Javascript into Java using ScriptEngine
  • Java replaceAll() method to escape special characters
  • Java (Removing a 'keyword' from the alphabet)
  • Format BigDecimal in Spring
  • iterating checkbox values stored in array using EL
  • Java - Jackcess API with .accde(MS Access) format
  • Alternative for some of Struts 1 methods in Struts 2
  • javax.crypto JDK source code, again
  • Spring Roo - Command 'service' not found
  • antlr4: ATN version 2 expected 3
  • Deploy GlassFish using Netbeans generate java.net.MalformedURLException: Bad URL path
  • why is wait() called for loop in Thread.join() of Java?
  • Java JAXB marshall into DOM Document
  • Why JDBI 3 @ColumnName annotation doesn't work?
  • Java SimpleDateformatter with 10 decimals after the seconds, cannot convert to Date
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org