2 separated objects are connected for some reason - java

This is my homework(assignment). I really hope someone can help me figure out what I did wrong.
When I tried to create an object of State
state = new State(tablesize, tb);
and then tried to make a copy
State st2 = new State(state);
and then tried to modify the data in state
state.placeNumber(1,1,3);
for some reason, the data in st2 is also changed.
Below is the code. I really hope someone can point out where my mistake is.
Thanks
public class State
{
private int arraysize;
private int lastfilledx;
private int lastfilledy;
private int table[][];
//constructor
public State()
{
}
public State(State s)
{
arraysize = s.getsize();
lastfilledx = s.lastindex_x();
lastfilledy = s.lastindex_y();
table = s.gettable();
}
public State(int size, int[][] tb)
{
arraysize = size;
table = new int[size][size];
//copy the initial table which is a 2d array
table = tb;
for (int i = 0 ; i < size; i++)
{
for(int j = 0 ; j < size ; j++)
{
if ( table[i][j] == 1)
{
lastfilledx = i;
lastfilledy =j;
break;
}
}
}
}
public void placeNumber(int i, int j, int nextvalue)
{
lastfilledx = i;
lastfilledy = j;
table[i][j] = nextvalue;
}
public void printoutput()
{
for (int i=0; i < arraysize; i++)
{
for (int j=0; j < arraysize; j++)
System.out.print(" " + table[i][j]);
System.out.println("");
}
System.out.println("last fill " + lastfilledx + " " + lastfilledy);
}
public int[][] gettable()
{
return table;
}
public int getsize()
{
return arraysize;
}
public int lastindex_x()
{
return lastfilledx;
}
public int lastindex_y()
{
return lastfilledy;
}
}
public class Dikuho extends State
{
private static State state;
public static void main(String[] args) {
int tablesize = 3;
int tb[][] = new int[tablesize][tablesize];
/*****HARDCODE the table data***/
for (int i=0; i < tablesize; i++)
for (int j=0; j < tablesize; j++)
tb[i][j] = 0;
//test case for 3x3
tb[2][2] = 1;
tb[0][0] = tablesize*tablesize;
tb[0][1] = 7;
tb[1][0] = 8;
tb[2][1] = 2;
//initialize the state
state = new State(tablesize, tb);
**//Here is where the problem is. I only change the data in state but the data in st2 is also changed. I'm not sure what happen here.**
State st2 = new State(state);
state.placeNumber(1,1,3);
st2.printoutput(); **//These both printout same output which is not correct**
state.printoutput();
}
}

Your copy constructor has made a shallow copy of table 2D array. Both the original object and the copy refer to the same original array, because you assign the array reference from the original to the new object. That's fine for the int values, because the values are copied. But that's not okay for objects, for which references to the obejct are copied.
Instead of just copying the reference to the array...
table = s.gettable();
You'll need to create a new, copied array:
table = new int[arraysize][arraysize];
// 2 nested for loops here to copy the contents

public int[][] gettable()
{
return table;
}
So, both of your state objects are referencing the same array. you need to create a new array for each instance.

Related

Bubble Sort an Array

I have an add point method, which adds a slot and puts a point inside. After adding the slot I want to sort what is in the array. How do I do that? Here is my code:
public void add(Point p)
{
int k;
for (int j = 0;j<myConsumerCurve.length;j++)
for (k=j+1;k<myConsumerCurve.length;k++)
if (k!=j && myConsumerCurve[k] == myConsumerCurve[j]) {
}
Point[] tempCurve = new Point [myConsumerCurve.length +1];
for(int i=0; i < myConsumerCurve.length; i++)
{
tempCurve[i] = myConsumerCurve[i];
}
tempCurve[myConsumerCurve.length] = p;
myConsumerCurve = tempCurve;
bubblesort(myConsumerCurve);
}
public static void bubblesort(Point[] myConsumerCurve)
{
}

Magic Square Method needs to output if it is a magic square and the sum of rows and columns

I'm working on an assignment that takes a data file with a number matrix and determines if it is a magic square. If it is then it also needs to report the sum of the rows and columns. With the output:
The matrix is a magic square.
The sum of all the rows and columns is 34.
I'm not sure how to go about this with one method, I feel like its asking me to return 2 values. The closest I have came is by adding a System.out.println with the sum at the end of my method when it returns true.
But the issue with that is that my output is backwords:
The sum of all the rows and columns is 34.
The matrix is a magic square.
How do I get the sum when I've only been asked to create one method? Below is my code, the instructor gave the bottom 3 methods so I'm only concerned with the first 2.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class chp8magic
{
public static void main(String args[])
{
int matrix[][] = initMatrix();
printData(matrix);
if (isMagic(matrix)) {
System.out.println("The matrix is a magic square.");
}
else {
System.out.println("Not a magic square");
}
}
public static boolean isMagic(int[][] mat)
{
int n = mat.length;
int nSquare = n*n;
int M = (n*n*(n*n+1)/2)/n;
int sumRow = 0, sumColoumns = 0, sumPriDiag = 0, sumSecDiag = 0;
boolean[] flag = new boolean[n*n];
for(int row = 0; row < n; row++){
sumRow = 0;
sumColoumns = 0;
for(int col = 0; col < n; col++)
{
if( mat[row][col] < 1 || mat[row][col] > nSquare )
return false;
if(flag[mat[row][col]-1] == true)
return false;
flag[mat[row][col]-1] = true;
sumRow += mat[row][col];
sumColoumns += mat[col][row];
}
sumPriDiag += mat[row][row];
sumSecDiag += mat[row][n-row-1];
if(sumRow!=M || sumColoumns!=M)
return false;
}
if(sumPriDiag!=M || sumSecDiag!=M)
return false;
else
return true;
}
public static int[][] initMatrix()
{
int matrix[][];
Scanner filein = null;
try {
filein = new Scanner(new File("matrix.txt"));
int numRows = Integer.parseInt(filein.nextLine());
matrix = new int[numRows][];
parseData(matrix, filein);
filein.close();
return matrix;
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
if(filein != null)
filein.close();
return null;
}
}
public static void parseData(int matrix[][], Scanner in)
{
for(int r = 0; r < matrix.length; r++)
{
String splitLine[] = in.nextLine().split(" ");
matrix[r] = new int[splitLine.length];
for(int c = 0; c < matrix[r].length; c++){
matrix[r][c] = Integer.parseInt(splitLine[c]);
}
}
}
public static void printData(int matrix[][])
{
for(int r = 0; r < matrix.length; r++){
for(int c = 0; c < matrix[r].length; c++){
System.out.print(matrix[r][c] + " ");
}
System.out.println();
}
}
}
Probably you just need to do:
System.out.println("is magic: " + isMagic);
System.out.ptintln("sum: " + sum);
However this is not really returning the values, just printing them. To return two values there are several options. You could return an object:
public class MagicSquareProperties {
private boolean magic;
private int sum;
public MagicSquareProperties(boolean magic, int sum) {
this.magic = magic;
this.sum = sum;
}
public boolean isMagic() {
return magic;
}
public int getSum() {
return sum;
}
}
// now to return both values: return new MagicSquareProperties(true, 34);
But in my opinion the best solution would be the following:
Create a class MagicSquare.
Add property declarations (e.g. private int sum).
Add a constructor, for example MagicSquare(File file), this constructor reads the file and populates the properties.
Add getters and setters if needed (e.g. getSum())
Execute the main method in another class and call new MagicSquare(...) to create a magic square object. Afterwards you can call the getters on the object whenever you need the value.

I am getting null values in my Array

I am playing around with an ArrayList and trying to get it to grow twice it's size every time it exceeds it's size. Here is my add method:
public class ArrayExpander
{
private int size;
private int noOfItems;
private Object[] store;
private final int INITIALSIZE = 2;
public ArrayExpander()
{
store = new Object[INITIALSIZE];
noOfItems = 0;
size = INITIALSIZE;
}
public void add(Object obj)
{
growBufferIfNecessary();
store[size++] = obj;
/*for (int i = size - 1; i < store.length; i++)
{
store[i] = store[i - 1];
store[i] = obj;
}*/
}
public String toString()
{
String temp = "[" + store[0];
for (int i = 1; i < size; i++)
{
temp = temp + "," + store[i];
}
temp = temp + "]";
return temp;
}
private void growBufferIfNecessary()
{
if (size == store.length)
{
Object[] newStore = new Object[2 * store.length];
for (int i = 0; i < store.length; i++)
{
newStore[i] = store[i];
}
store = newStore;
}
}
public static void main(String[] args)
{
ArrayExpander ae = new ArrayExpander();
//System.out.println(ae);
ae.add("a");
ae.add("b");
System.out.println(ae);
ae.add("c");
ae.add("d");
ae.add("e");
ae.add("f");
ae.add("g");
ae.add("h");
System.out.println(ae);
ae.add("i");
System.out.println(ae);
}
}
Here is my output:
[null,null]
[null,null,a,b]
[null,null,a,b,c,d,e,f,g,h]
[null,null,a,b,c,d,e,f,g,h,i]
I can't figure out why I am getting the null statements. The first line should be a,b and then the arraylist should double in size and be a,b,c,d. I have it set for final int INITIALSIZE = 2.
The output I am looking for is
[a,b]
[a,b,c,d]
[a,b,c,d,e,f,g,h]
[a,b,c,d,e,f,g,h,i,null,null,null,null,null,null,null]
This code will work for you. size should be referring to the size of your array while noOfItems refers to the number of items in your array. You were kind of mixing the 2 up. I only changed a couple things in your add() and growBufferIfNecessary().
public class ArrayExpander
{
private int size;
private int noOfItems;
private Object[] store;
private final int INITIALSIZE = 2;
public ArrayExpander()
{
store = new Object[INITIALSIZE];
noOfItems = 0;
size = INITIALSIZE;
}
public void add(Object obj)
{
growBufferIfNecessary();
store[noOfItems++] = obj;
}
public String toString()
{
String temp = "[" + store[0];
for (int i = 1; i < size; i++)
{
temp = temp + "," + store[i];
}
temp = temp + "]";
return temp;
}
private void growBufferIfNecessary()
{
if (noOfItems == size)
{
size = 2 * size;
Object[] newStore = new Object[size];
for (int i = 0; i < store.length; i++)
{
newStore[i] = store[i];
}
store = newStore;
}
}
public static void main(String[] args)
{
ArrayExpander ae = new ArrayExpander();
//System.out.println(ae);
ae.add("a");
ae.add("b");
System.out.println(ae);
ae.add("c");
ae.add("d");
ae.add("e");
ae.add("f");
ae.add("g");
ae.add("h");
System.out.println(ae);
ae.add("i");
System.out.println(ae);
}
}
Try this. If you notice I replaced size in a couple spots with noOfItems. You were really close you just needed to change a couple things.
Manually copying arrays with loops is such a pain, use System.arraycopy(Object,int,Object,int,int) like
private int size = 0;
private Object[] store = new Object[INITIALSIZE];
private void growBufferIfNecessary() {
if (size >= store.length) {
Object[] newStore = new Object[2 * store.length];
System.arraycopy(store, 0, newStore, 0, store.length);
store = newStore;
}
}
I eliminated noOfItems. You don't need it, your add method is just
public void add(Object obj) {
growBufferIfNecessary();
store[size++] = obj;
}
Finally, your toString() could use Arrays.copyOf(T[], int) like
#Override
public String toString() {
return Arrays.toString(Arrays.copyOf(store, size));
}
And then I got your expected output
[a, b]
[a, b, c, d, e, f, g, h]
[a, b, c, d, e, f, g, h, i]

Java, filling Object[][] array with data

I'm trying to fill an Object[][] array with data from my Object Class. However im having problems filling the array. Below is what I am trying to do to fill the Object[][] data. at the moment the returned data variable cannot be seen by the method. I have tried removing the method and filling the array where rows in declared but cannot because there is a for loop.
Am I currently filling the object[][] array correctly?
public class CustomersDialog extends javax.swing.JDialog {
private CustomerList customers = new CustomerList();
Object rows[][] = getData();
public Object[][] getData() {
customers = dataManager.getUserData();
int size = customers.size();
Customer customer = new Customer();
for(int i = 0; i < size; i++) {
customer = customers.getCustomerAt(i);
Object [][] data = {
{ Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() } };
}
return data;
}
}
Further doing this method of creating the array outside the loop causes an 'empty statement message' by the compiler and it says it 'requires line ends ; after the .get statements':
public Object[][] getData() {
customers = dataManager.getUserData();
int size = customers.size();
Customer customer;
Object [][] data;
for(int i = 0; i < size; i++) {
customer = customers.getCustomerAt(i);
data = {
{ Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() } };
}
return data;
}
You're not doing it properly.
In your code, you're declaring an array INSIDE for loop, which means that after the loop, the array doesn't exist anymore. That's why you can't return data - it simply does not exist.
More about scope and lifetime of variables you can read there: http://www.c4learn.com/javaprogramming/the-scope-and-lifetime-of-variables-in-java-programming-language/
What you want to do is to declare array outside the loop:
Object [][] data;
for(int i; i < size; i++) {
// Filling data array
}
return data;
Next thing is that if you want to use an array, you should initialize it first:
Object [][] data = new Object [size][3];
Then you can fill it in for loop, like this:
for(int i; i < size; i++) {
customer = customers.getCustomerAt(i);
data[i][0] = Integer.toString(customer.getCustomerID());
data[i][1] = customer.getfName();
data[i][2] = customer.getlName();
}
You have to allocate the array in two times. Once for the rows, and after once per row.
Object[][] data = new Object[size];
for(int i = 0; i < size; i++) {
customer = customers.getCustomerAt(i);
data[i] =
new Object[]{
Integer.toString(customer.getCustomerID()),
customer.getfName(),
customer.getlName()
};
}
If you really want to use a 2 dimensional Array than you must define the size of the Array first (also if it is a n dimensional Array!)
See this content to get some clarification:
http://www.leepoint.net/notes-java/data/arrays/arrays-2D.html
It seems to me like you only have one size Parameter for the Array, so I would use a normal Array instead of a 2 dimensional one.
I would do something like this:
public Object[] getData() {
customers = dataManager.getUserData();
int size = customers.size();
Object[] result = new Object[size];
Customer customer;
for(int i = 0; i < size; i++) {
customer = customers.getCustomerAt(i);
result[i] = {
{ Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() } };
}
return data;
}
If you have to use a 2 dimensional Object Array, than you have to define the second size dimension and fill the array like this:
for(int i = 0; i < sizeDimOne; i++) {
for(int k = 0; k < sizeDimTwo; k++) {
result[i][k] = { { Integer.toString(customer.getCustomerID()), customer.getfName(), customer.getlName() } };
}
}
Hope this is helpful.

Converting an ArrayList into a 2D Array

In Java how do you convert a ArrayList into a two dimensional array Object[][]?
From comments: I will describe you the problem with more details: an XML file includes a list of contacts (e.g. name, address...). The only way I can obtain this information is through an ArrayList, which will be given to me. As I need to store the content of this array list in a Java Swing table in an ordered manner, I was thinking to convert it into a two dimensional array of objects
I presume you are using the JTable(Object[][], Object[]) constructor.
Instead of converting an ArrayList<Contact> into an Object[][], try using the JTable(TableModel) constructor. You can write a custom class that implements the TableModel interface. Sun has already provided the AbstractTableModel class for you to extend to make your life a little easier.
public class ContactTableModel extends AbstractTableModel {
private List<Contact> contacts;
public ContactTableModel(List<Contact> contacts) {
this.contacts = contacts;
}
public int getColumnCount() {
// return however many columns you want
}
public int getRowCount() {
return contacts.size();
}
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0: return "Name";
case 1: return "Age";
case 2: return "Telephone";
// ...
}
}
public Object getValueAt(int rowIndex, int columnIndex) {
Contact contact = contacts.get(rowIndex);
switch (columnIndex) {
case 0: return contact.getName();
case 1: return contact.getAge();
case 2: return contact.getTelephone();
// ...
}
}
}
Later on...
List<Contact> contacts = ...;
TableModel tableModel = new ContactTableModel(contacts);
JTable table = new JTable(tableModel);
The simple way is to add a method to the Contact like this:
public Object[] toObjectArray() {
return new Object[] { getName(), getAddress, /* ... */ };
}
and use it like this:
ArrayList<Contact> contacts = /* ... */
Object[][] table = new Object[contacts.size()][];
for (int i = 0; i < contacts.size(); i++) {
table[i] = contacts.get(i).toObjectArray();
}
Try this:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(..);
list.add(..);
list.add(..);
list.add(..);
list.add(..);
list.add(..);
int[][] a = new int[list.size()][list.size()];
for(int i =0; i < list.size(); i++){
for(int j =0; j <list.size(); j++){
a[i][j]= list.get(j +( list.size() * i));
}
}
I managed to find "a way" to do so, knowing the number of attributes each contacts has (6). So considering an ArrayList listofContacts
int numberOfContacts = listofContacts.size()/6;
Object[][] newArrayContent = new Object[numberOfContacts][6];
for(int x = 0; x<numberOfContacts; x++){
for(int z = 0; z < 6; z++){
int y = 6 * x;
newArrayContent [x][z] = list.get(y+z);
System.out.println(newArrayContent [x][z].toString());
}
}
What you really want is to sort the ArrayList. To do that your Contacts class must implement a Comparator method.
Check the next page for an example: http://www.java-examples.com/sort-java-arraylist-descending-order-using-comparator-example
I will recommend that you parse your XML into java objects and store the object in a custom data object. This will make it easier for you to do many operations on the available data.
Here is small tutorial on how to do it.
public static String[][] convertListIntoArrayObj(List<TeamMenuSelected> possibilities) {
int numberOfColums = 2;
int numberOfRows = possibilities.size();
String[][] values = new String[numberOfRows][numberOfColums];
for(int x=0; x<possibilities.size(); x++) {
TeamMenuSelected item = possibilities.get(x);
values[x][0] = item.getTeamName();
values[x][1] = item.getTeamCuisine();
}
return values;
}
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("element_1");
arrayList.add("element_2");
arrayList.add("element_3");
arrayList.add("element_4");
int k=0;
int row = 2, col = 2;
Object[][] objArray = new Object[row][col];
for(int i = 0 ; i < row; i++) {
for(int j = 0; j < col; j++) {
objArray[i][j] = arrayList.get(k);
k++;
if(k > arrayList.size()) {
break;
}
}
}
for(int i = 0 ; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.println("Row no "+i+" col no "+j+" "+objArray[i][j] );
}
}
}

Categories

Resources