Any changes made to an object, changes also the current object - java

Sorry, the title is not very understandable, but my English is not helping. I am a new programmer in java, and despite having read how parameters work, I do not really understand what is happening.
sudokuBoard alter = new sudokuBoard();
this.createRandomSudokuBoard();
alter.setBoardFromArray(this.getBoard().clone());
(...)
for(int i = 0; i < 81; i++) {
alter.clearCell(positionListonX[i], positionListonY[i]); <<<<<<<<<<<<< Here
if(alter.numberOfSolutions(2) < 2) {
this.clearCell(positionListonX[i], positionListonY[i]);
alter.setBoardFromArray(this.getBoard().clone());
} else {
alter.setBoardFromArray(this.getBoard().clone());
}
}
What happens is that in the indicated line, calling the method clearCell of the object alter is also modifying the current object (this). In a last desperate attempt, I tried to resolve it with the clone() method (as you can see), but it did not work.
What's going on? What am I missing? thank you very much.

If you haven't implemented clone() in SudokuBoard, then you're probably getting the default clone() defined on Object, which doesn't perform a deep copy of the object. See Deep Copy for an explanation. If you actually want a completely separate instance of your board in alter, you will need to do something like this:
class SudokuBoard
{
public void setBoard( SudokuBoard other )
{
for( int i = 0; i < 81; i++ )
{
this.positionListonX[i] = other.positionListonX[i];
this.positionListonY[i] = other.positionListonY[i];
}
// Copy any other properties
}
}
Note that if the values in your positionListonX and positionListonY arrays are not primitive types, you'll also need deep copies of those. This is effectively a copy constructor, but I didn't give it that signature (public SudokuBoard( SudokuBoard other)) because I don't know the rest of the internals of SudokuBoard.
It would help to see more of the method signatures defined in your SudokuBoard class, so we know what methods are available and can understand what they do.
Edit
class SudokuBoard
{
public void setBoardFromArray( int[][] otherBoard )
{
for( int i = 0; i < otherBoard.length; i++ )
{
// Clone the arrays that actually have the data
this.board[i] = otherBoard[i].clone();
}
}
}

Related

Is there a way to loop execute a method while passing a different instance of a class to it each time?

My current code looks something like this:
public void myMethod()
{
instance1.myPanel.setVisible();
instance2.myPanel.setVisible();
instance3.myPanel.setVisible();
instance4.myPanel.setVisible();
//A bunch more
instance57.myPanel.setVisible();
}
Is there a ways to shorten it?
The Code below obviously doesn't work but gives you an idea of what I'm trying to do:
public void myMethod2(myClass instance1)
{
instance1.myPanel.setVisible();
}
int i = 1;
while(i <= 57)
{
myMethod2("instance" + i);
i++
}
In practice, this kind of problem is normally handled using some sort of collection, and its use will often fit naturally into the initialization of a program with a large number of objects. Rather than hand-writing the creation of 57 similar objects, one line at a time, you would create them in a loop, adding them to a collection as you do so.
A List implementation like ArrayList would be a good choice here, or one could simply use an array.
With a List:
/* During initialization of your program somewhere. */
List<MyClass> instances = new ArrayList<>();
for (int i = 0; i < 57; ++i) {
instances.add(new MyClass());
}
...
/* Later when you need to invoke a method: */
instances.forEach(instance -> instance.myPanel.setVisible());
You can build a list of your vars and just go through them:
List<ClassType> list = Arrays.asList( obj1, ob2, obj3 );
list.forEach( instance -> instance.myPanel.setVisible() );

Remove object from object ArrayList

I have created an object ArrayList,
private ArrayList<Object> objects;
and I am initializing it in a constructor.
public ObjectManager(Handler handler) {
this.handler = handler;
objects = new ArrayList<>();
}
This ArrayList is then painted/added it to a canvas.
public void renderObjects(Graphics g) {
handler.getObjectManager().addObject(new InstanceOfObject(handler, 1000, 1000, g));
}
The method addObject(), adds an object to the ArrayList.
public void addObject(Object e) {
objects.add(e);
}
I would like to remove this object later, by using a similar line of code,
public void removeObject(Object e) {
objects.remove(e);
}
however I do not know how to do that because I do not know how to pass in the object that is being removed. The only way I can think of passing in the object is by doing the following:
handler.getObjectManager().removeObject(new InstanceOfObject(handler, 1000, 1000, g));
I don't even know if this would work because it's removing an "new" object. And even if it does, "g" is not defined. If I define it in the constructor, I have to change many different things which results in an error (usually a NullPointerException), but even then I cannot figure out how to call this method by passing in the Graphics g parameters.
Your Question is not clear, but this might help.
The List interface implemented by ArrayList already offers a remove method. No need for you to re-invent that.
Object reference
To remove an object, keep and pass a reference to the particular object.
Dog alice = new Dog( "Alice" , "Labrador" ) ;
Dog bob = new Dog( "Bob" , "Chihuahua" ) ;
List< Dog > dogs = new ArrayList<>() ;
dogs.add( alice ) ;
dogs.add( bob ) ;
…
dogs.remove( bob ) ;
Index number
Alternatively, remember the slot (index) of the list containing the object you want to remove. Pass that zero-based index number to the remove method.
You can actually find Java's source code on the web (like https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/util/ArrayList.java#L644), or even as src.zip in the JDK itself. So this is how remove() looks like:
public boolean remove(Object o) {
final Object[] es = elementData;
final int size = this.size;
int i = 0;
found: {
if (o == null) {
for (; i < size; i++)
if (es[i] == null)
break found;
} else {
for (; i < size; i++)
if (o.equals(es[i]))
break found;
}
return false;
}
fastRemove(es, i);
return true;
}
and while the loops with the labeled breaks may look a bit esoteric, the important part is the o.equals(): if your "InstanceOfObject" class implements its own equals(), you can make the comparison work with freshly made throwaway instances too.

Initialising/Creating Objects through a constructor

Ok so I'm doing an assignment for my java coursets part I'm stuck at is :
"Implement an operation createparliamentMembers which will create the particular Parliament
with 80 members."
So i've already created the constructor with it's methods. This is how I wrote the operation to create the objects using the constructor.:
public static void createparliamentMembers(){
Member[] array = new Member[75];
for(int i = 0; i < array.length; i++)
{
if (i < 35) array[i] = new Member(i, "Blue");
else array[i] = new Member(i,"Red");
}
Legislator[] leg = new Legislator[3];
for (int i = 0 ; i < leg.length; i++){
leg[i] = new Legislator(i, "Impartial");
}
Leader[] lead = new Leader[2];
for (int t = 0; t < lead.length; t++){
if (t < 1) lead[t] = new Leader(1, "Red");
else lead[t] = new Leader(2, "Blue");
}
The problem is the arrays and objects only seem to exist in the operation for creating them and when I try running method of the objects created they don't work because the driver class doesn't recognize the arrays. On the other hand when I use this as just a normal part of the Driver for it runs fine and all methods of the objects work normally.
Edit: Ok so I'm still getting the same problem as before even though i initiliased them outside the createparliamentMembers();
The following code is the Driver im using to test the methods: It keeps saying there is a:
Exception in thread "main" java.lang.NullPointerException at Driver.main(Driver.java:11)
which is the code array[1].FlipCoin(); as im trying to use the method flipcoin from the created objects but it's not working.
public static void main(String [] args) {
Commands.createparliamentMembers();
array[1].FlipCoin();
}
Your arrays are only defined locally, which means they live and die with the method. When your method finishes, they get put out of memory.
The solution is to define these arrays as instance variables. By that I mean, you need to define the arrays for your class, and then use them in your method:
class someClass {
int[] myArray = new int[2];
private void someMethod() {
myArray[0] = 3;
myArray[1] = //whatever
}
}
You state in comment:
I do have a parliament class it's on it own and contains the methods and constructor for the members of the parliament. The above method was in a seprate class called Commands. I don't understand completely the "Can you add the members to a Parliament object as you create them?" The parliament isn't an object more se then a class containing a constructor and methods for parliament members i want to create.
Parliament isn't an object yet, but you should in fact create one, and in fact your instructions tell you just that: "which will create the particular Parliament with 80 members...". You will need to tell us more about your program's structure and your specific requirements, but I suggest:
First create a Parliament object in the createParliamentMembers method, and call it parliament.
Then create the members of parliament in that method.
As you create these members, add them to the Parliament object, parliament.
At the end of the method return the parliament variable.
This means that your createParliamentMembers method's signature must change so that rather than return void it should be written to return a Parliament object.
When calling the method in the main method, assign what it returns to a Parliament variable that is in the main method.
It looks like you are writing a factory method. Create a constructor for Parliament like this:
public Parliament(Member[] members, Legislator[] legislators, Leader[] leaders) {
// do whatever with what's passed in
}
Then change your method to return a Parliament object and in the method pass your initialized arrays into the Parliament constructor, like this:
// same code as your except the last line
public static Parliament createParliament(){
Member[] array = new Member[75];
for(int i = 0; i < array.length; i++)
{
if (i < 35) array[i] = new Member(i, "Blue");
else array[i] = new Member(i,"Red");
}
Legislator[] leg = new Legislator[3];
for (int i = 0 ; i < leg.length; i++){
leg[i] = new Legislator(i, "Impartial");
}
Leader[] lead = new Leader[2];
for (int t = 0; t < lead.length; t++){
if (t < 1) lead[t] = new Leader(1, "Red");
else lead[t] = new Leader(2, "Blue");
}
return new Parliament(array, leg, lead);
}

method undefined for type Java, nested classes

I'm running into some trouble with nested classes on a project for school.
Currently, I'm trying to write a method to insert an item into a ragged array data structure.
It uses an object created by a nested class to keep track of the 2d array location in order to get the indexes to insert at. However, I am getting the error "The method findEnd(E) is undefined for the type RaggedArrayList.ListLoc" on the line:
insertLoc.findEnd(item)
I have searched extensively both on stackoverflow as well as around the web and have not found the answer yet. If I have missed it and this is redundant (there are a lot of "method undefined for type questions", I know) then I apologize.
Here is the relevant code >>
nested class for ListLoc object:
private class ListLoc {
public int level1Index;
public int level2Index;
public ListLoc() {}
public ListLoc(int level1Index, int level2Index) {
this.level1Index = level1Index;
this.level2Index = level2Index;
}
public int getLevel1Index() {
return level1Index;
}
public int getLevel2Index() {
return level2Index;
}
// since only used internally, can trust it is comparing 2 ListLoc's
public boolean equals(Object otherObj) {
return (this == otherObj);
}
}
Method to find the last index of a matching item (not part of ListLoc nested class):
private ListLoc findEnd(E item){
E itemAtIndex;
for (int i = topArray.length -1; i >= 0; i--) {
L2Array nextArray = (L2Array)topArray[i];
if (nextArray == null) {
continue;
} else {
for (int j = nextArray.items.length -1; j >= 0; j--) {
itemAtIndex = nextArray.items[j];
if (itemAtIndex.equals(item)) {
return new ListLoc(i, j+1);
}
}
}
}
return null;
}
Method attempting to add a new value to the ragged array:
boolean add(E item){
ListLoc insertLoc = new ListLoc();
insertLoc.findEnd(item);
int index1 = insertLoc.getLevel1Index();
int index2 = insertLoc.getLevel2Index();
L2Array insertArray = (L2Array)topArray[index1];
insertArray.items[index2] = item;
return true;
}
Thanks for any input.
I'm going to bet that changing this:
ListLoc insertLoc = new ListLoc();
insertLoc.findEnd(item);
To this:
ListLoc insertLoc = findEnd(item);
Will fix your problem.
You're trying to call findEnd on the ListLoc class, but if you actually look at ListLoc, it's not defined there. When you tried to call findEnd on the insertLoc object, it fails because insertLoc is an instance of ListLoc, which we already said doesn't contain findEnd.
That being said, I'm going to bet that findItem is actually declared in the same class as the add method (let's just call it MyList), so you wanted to actually call MyList.findEnd, not the non-existent ListLoc.findEnd.
You pretty much answered your own question.. You're trying to call findEnd on a ListLoc object, but ListLoc doesn't define a findEnd method. You need to either
a) Add an implementation for findLoc to ListLoc or
b) Call findLoc on the proper object (you haven't provided information about the other class so I can't say too much about it)
Method to find the last index of a matching item (not part of ListLoc nested class):
Right - it's not part of ListLoc... but when you call it here:
ListLoc insertLoc = new ListLoc();
insertLoc.findEnd(item);
... you're trying to call it as if it is part of the class. It isn't, so you can't call it like that.
Either move it into ListLoc, or change the way you're calling the method.
I am not sure if I am reading this correct, but from your explanation is looks like the findEnd method is defined outside of your class, therefore ListLoc indeed doesn't have a method by that name...
Where is your private ListLoc findEnd(E item) defined?

Is it possible to loop setters and getters?

I'm fairly confident that there's no way this could work, but I wanted to ask anyway just in case I'm wrong:
I've heard many times that whenever you have a certain number of lines of very similar code in one batch, you should always loop through them.
So say I have something like the following.
setPos1(getCard1());
setPos2(getCard2());
setPos3(getCard3());
setPos4(getCard4());
setPos5(getCard5());
setPos6(getCard6());
setPos7(getCard7());
setPos8(getCard8());
setPos9(getCard9());
setPos10(getCard10());
setPos11(getCard11());
setPos12(getCard12());
There is no way to cut down on lines of code as, e.g., below, right?
for (i = 0; i < 12; i++) {
setPos + i(getCard + i)());
}
I'm sure this will have been asked before somewhere, but neither Google nor SO Search turned up with a negative proof.
Thanks for quickly confirming this!
No way to do that specifically in Java without reflection, and I don't think it would be worth it. This looks more like a cue that you should refactor your getcard function to take an integer argument. Then you could loop.
This is a simple snippet that shows how to loop through the getters of a certain object to check if the returned values are null, using reflection:
for (Method m : myObj.getClass().getMethods()) {
// The getter should start with "get"
// I ignore getClass() method because it never returns null
if (m.getName().startsWith("get") && !m.getName().equals("getClass")) {
// These getters have no arguments
if (m.invoke(myObj) == null) {
// Do something
}
}
}
Like the others stated, probably it's not an elegant implementation. It's just for the sake of completeness.
You could do it via reflection, but it would be cumbersome. A better approach might be to make generic setPos() and getCard() methods into which you could pass the index of the current item.
You need to ditch the getter/setter pairs, and use a List to store your objects rather then trying to stuff everything into one God object.
Here's a contrived example:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Foo {
public static class Card {
int val;
public Card(int val) {
this.val = val;
}
public int getVal() {
return val;
}
}
public static class Position {
int value;
public Position(Card card) {
this.value = card.getVal();
}
}
public static void main(String[] args) {
List<Card> cards = new ArrayList<Card>(Arrays.asList(new Card(1), new Card(2), new Card(3)));
List<Position> positions = new ArrayList<Position>();
for (Card card : cards) {
positions.add(new Position(card));
}
}
}
You can't dynamically construct a method name and then invoke it (without reflection). Even with reflection it would be a bit brittle.
One option is to lump all those operations into one method like setAllPositions and just call that method.
Alternatively, you could have an array of positions, and then just loop over the array, setting the value at each index.
Card[] cardsAtPosition = new Card[12];
and then something like
public void setCardsAtEachPosition(Card[] valuesToSet) {
// check to make sure valuesToSet has the required number of cards
for (i = 0; i < cardsAtPosition.length; i++) {
cardsAtPosition[i] = valuesToSet[i];
}
}
Reflection would be your only option for your example case.

Categories