I am trying to clone an object without using any library.
The object has other objects/arrays/matrixes in it.
So I developed some methods to clone those as well.
They are working fine when I am cloning arrays/matrixes that are not inside the object.
These are the methods:
public static int[] cloneArray(int[] array){
int i = 0;
int[] clone = new int[array.length];
while (i < array.length){
clone[i] = array[i];
i++;
}
return clone;
}
public static int[][] cloneMatrix(int[][] matrix){
int[][] clone = new int[matrix.length][matrix[0].length];
for (int i = 0;i<matrix.length;i++)
for(int j = 0;j<matrix[0].length;j++)
clone[i][j] = matrix[i][j];
return clone;
}
However, when I want to clone an object, the references of the array/matrix stay the same, as you can check in the output on the bottom of the post.
This is the constructor I have:
public State(int parentStateID, int stateID, int[][] board, int[] pieces, int points, int acquiredPoints){
State.parentStateID = parentStateID;
State.stateID = stateID;
State.currentBoard = cloneMatrix(board); //here takes place the matrix cloning
State.currentPieces = cloneArray(pieces); //here takes place the array cloning
State.totalPoints = points;
State.acquiredPoints = acquiredPoints;
}
And this is the cloning method:
public static State cloneState(State state){
int[][] currentBoard = state.getCurrentBoard();
int[] currentPieces = state.getCurrentPieces();
int totalPoints = state.getTotalPoints();
int acquiredPoints = state.getAcquiredPoints();
int parentStateID = state.getParentStateID();
int stateID = state.getStateID();
State clone = new State(parentStateID,
stateID,
currentBoard,
currentPieces,
totalPoints,
acquiredPoints);
return clone;
}
To better visualize the output, here are the arrays and matrix:
public static int piecesList[] = {1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
public static int piecesList2[] = {2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
public static int piecesList3[] = {3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
private static int[][] map = {{0,1,2,3,4},{5,6,7,8,9},{10,11,12,13,14},{15,16,17,18,19},{20,21,22,23,24}};
Here is the verification code:
int[][] state2 = cloneMatrix(map);
state2[0][0] = 1;
System.out.println("map.original " + map[0][0]);
System.out.println("map.clone " + state2[0][0]);
System.out.println("");
System.out.println("");
int[] pc = cloneArray(piecesList);
pc[24] = 1;
System.out.println("pieces.original " + piecesList[24]);
System.out.println("pieces.clone " + pc[24]);
System.out.println("");
System.out.println("");
State newState = setFirstState();
State clonedState = cloneState(newState);
clonedState.setCurrentPieces(piecesList2);
System.out.println("newState.pieceslist: "+newState.getCurrentPieces()[0]);
System.out.println("clonedState.pieceslist: "+clonedState.getCurrentPieces()[0]);
System.out.println("piecesList.original: "+piecesList[0]);
System.out.println("");
newState.setCurrentPieces(piecesList3);
System.out.println("newState.pieceslist: "+newState.getCurrentPieces()[0]);
System.out.println("clonedState.pieceslist: "+clonedState.getCurrentPieces()[0]);
System.out.println("piecesList.original: "+piecesList[0]);
And here is the output:
map.original 0
map.clone 1
pieces.original -1
pieces.clone 1
//as you can check in the next two cases, the change takes effect in both the original state, and the cloned state
newState.pieceslist: 2
clonedState.pieceslist: 2
piecesList.array variable: 1
newState.pieceslist: 3
clonedState.pieceslist: 3
piecesList.array variable: 1
Been trying to solve this issue for over 12 hours, without much success...
I have tried libraries as well as serialization with no success...
Help is much appreciated!
The problem with your code is that the fields (not shown) obviously are static, as can be told from your constructor:
public State(int parentStateID, int stateID, int[][] board, int[] pieces, int points, int acquiredPoints){
State.parentStateID = parentStateID;
State.stateID = stateID;
State.currentBoard = cloneMatrix(board); //here takes place the matrix cloning
State.currentPieces = cloneArray(pieces); //here takes place the array cloning
State.totalPoints = points;
State.acquiredPoints = acquiredPoints;
}
The constructor should instead read like this:
public State(int parentStateID, int stateID, int[][] board, int[] pieces, int points, int acquiredPoints){
this.parentStateID = parentStateID;
this.stateID = stateID;
this.currentBoard = cloneMatrix(board); //here takes place the matrix cloning
this.currentPieces = cloneArray(pieces); //here takes place the array cloning
this.totalPoints = points;
this.acquiredPoints = acquiredPoints;
}
A field which is static exists only once per class. A field which is not static exists once per each object constructed from that class (or any subclass). Because of that, you actually created objects which are empty, and whatever you thought you would store in the object you actually stored in the class, thus sharing the same data among all objects.
As a rule of thumb, you usually do not want any static non-final fields in your classes. Exceptions apply, but they're rare and mostly limited to a few tiny bootstrapping things in frameworks.
I have List<Vector3D> , where Vector3D is a coordinate. I want to find sum of all distance between Vector3D elements of list. I want to find it using java 8 streams. I try to use reduce but it cant help me.
UPD:
Class Vector3D has method double distance(Vector3D) witch find distance between two positions. e.g. i have list with (1,0,0) (2,0,0) (3,0,0). As a result i want to find length of this path. It is 3.
If we are using java 7 or lower we have to do:
public static double calcPathLength(List<Vector3D> path){
double length = 0d;
for (int i=0; i< path.size()-1; i++){
length += path.get(i).distance(path.get(i+1));
}
return length;
}
The operation you are performing is called Mutable reduction.
Pshemo’s answer shows how you can implement such operation ad-hoc by providing the three necessary functions. However, when all three functions are implemented by a dedicated class it might be useful to implement these functions inside a class implementing Collector for easier reuse:
public class Distance implements Collector<Vector3D, Distance.Helper, Double> {
public static final Distance COLLECTOR = new Distance();
static final class Helper {
private double sum = 0;
private Vector3D first = null, previous = null;
}
public Set<Characteristics> characteristics() {
return Collections.emptySet();
}
public Supplier<Helper> supplier() {
return Helper::new;
}
public BiConsumer<Helper, Vector3D> accumulator() {
return (helper,vector3d)-> {
if (helper.previous != null)
helper.sum += vector3d.distance(helper.previous);
else helper.first = vector3d;
helper.previous = vector3d;
};
}
public BinaryOperator<Helper> combiner() {
return (h1,h2)-> {
h2.sum += h1.sum;
if(h1.previous!=null && h2.first!=null) {
h2.sum += h1.previous.distance(h2.first);
h2.first=h1.first;
}
return h2;
};
}
public Function<Helper, Double> finisher() {
return helper -> helper.sum;
}
}
You will recognize the three function from the ad-hoc version. New is a fourth function, finisher which allows to specify how the final result can be extracted from the mutable container so we don’t need the getSum() call.
The use case simplifies to:
List<Vector3D> list;
//…
double distance=list.stream().collect(Distance.COLLECTOR);
One of the options would be creating some helper class which would remember previously used vector and based on it calculate difference between it and current vector. This class could look like
class DistanceHelper {
private double sum = 0;
private Vector3D first = null;
private Vector3D last = null;
public void add(Vector3D vector3d) {
if (first == null)
first = vector3d;
if (last != null)
sum += vector3d.distance(last);
last = vector3d;
}
public void combine(DistanceHelper otherHelper) {
//add distance of path from current thread with distance of path
//from other thread
sum += otherHelper.sum;
//also add distance between paths handled by separate threads like
// when path of Thread1 is A->B and Thread2 is C->D then we need to
// include path from `B` to `C`
if (this.last!=null && otherHelper.first!=null)
sum += this.last.distance(otherHelper.first);
this.last = otherHelper.last;
}
public double getSum() {
return sum;
}
}
and you can use it for example with combine instead of reduce like
double sum = list
.stream()//or parallelStream()
.collect(DistanceHelper::new, DistanceHelper::add,
DistanceHelper::combine).getSum();
I have an array of objects. When the array fills up, I want to make a new array twice as large as the old one, and transfer all the elements over. I'm doing something wrong, I think its something to do with I'm not creating the correct reference to the new array. Here's my code, any help figuring this out would be appreciated.
private int DIRECTORY_SIZE = 6;
Entry [] directory = new Entry[DIRECTORY_SIZE];
private int numberOfElements = 0;
public int getNumOfElements(){
return numberOfElements;
}
public void setDirectorySize(int size){
DIRECTORY_SIZE = size;
}
public int getDirectorySize(){
return DIRECTORY_SIZE;
}
public void addEntry(String surname, String initial, String num) {
// TODO add an entry to an array, also increments numberOfElements variable tracking whats in array
if(getNumOfElements() == getDirectorySize()){ // if array is full
doubleArraySize(); // put temp values into new bigger directory array
}
int i = findFreeLocation();
directory[i] = new Entry(surname, initial, num);
numberOfElements++;
}
private void doubleArraySize(){
Entry[] temp = new Entry[DIRECTORY_SIZE]; //make new temp array same size as old one
for(int i = 0; i < DIRECTORY_SIZE ; i++){
temp[i] = directory[i]; // cycle through array putting all values into temp
// works up to here
}
setDirectorySize(DIRECTORY_SIZE*2); // double size of array
Entry[] directory = new Entry[DIRECTORY_SIZE]; // create new, double size directory array
for(int i = 0; i < temp.length ; i++){
directory[i] = temp[i];
}
}
private int findFreeLocation() {
int i;
for (i = 0; i < DIRECTORY_SIZE; i++)
{
if(directory[i] == null)
{
break;
}
}
return i;
}
In doubleArraySize() function , this is the issue :
Entry[] directory = new Entry[DIRECTORY_SIZE];
// you are not assigning it to the class attribute directory
// instead you are creating a local array directory
Make the following change :
this.directory = new Entry[DIRECTORY_SIZE];
// this will assign the newly created array to the class attribute
Note : I personally prefer to use this pointer to refer to class attributes so that it makes my code more readable, and its clear to everyone that the variable in question is a class attribute rather than local variable.
**SIZE has already double by this point. No need to multiple by 2
I remember doing something exactly like this when I was making a Vector ADT. However, I used instance variables instead of methods in my code for element number and the capacity. I definitely didn't initialize a Vector inside a method for a Vector.
setDirectorySize(DIRECTORY_SIZE*2); // double size of array
Entry[] directory = new Entry[DIRECTORY_SIZE]; // create new, double size directory array
Isn't DIRECTORY_SIZE an instance variable? Because if it is, I don't think you can initialize an object using an instance variable from the object you are overwriting.
Putting my code into your context, it would look something like this:
private void doubleDirectorySize()
{
Entry[] new_array = new Entry[new_directory_size*2];
for (int i = 0; i < directory_size; i++)
{
new_array[i]= directory[i];
}
directory= new_array;
}
This only works if directory was initialized to null, though, moving the pointer directory to the new array.
I've got two compile errors in one of my classes and I don't understand why they're there.
The top error is saying there needs to be another semi-colon and the bottom one says it needs another closing brace.
The bottom error disappears if i put in another curly brace but the top one doesn't. Any ideas?
(This is probably a case of me being blind/stupid so i apologise in advance :)
package com.pathfinding;
import java.util.ArrayList;
public class EdgeNodeFactory
{
static boolean[][] edgeMatrix = new boolean[100][100];
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
edgeMatrix[i][j] = false;
}
}
static ArrayList<Node> nodes = new ArrayList<Node>();
static ArrayList<Edge> edges = new ArrayList<Edge>();
static int edgeCount = 0;
static int nodeCount = -1;
}
You've tried to put code (the for loop) directly in your class - it's not in a constructor, a method, or a static/instance initializer. That's not valid. When do you want that code to be executed?
I suspect your code should really look like this:
public class EdgeNodeFactory
{
private boolean[][] edgeMatrix = new boolean[100][100];
private int edgeCount = 0;
private int nodeCount = -1;
private List<Node> nodes = new ArrayList<Node>();
private List<Node> edges = new ArrayList<Edge>();
public EdgeNodeFactory()
{
// You *could* put your for loop here... but the array
// elements will all be false anyway, as that's the default...
// If you don't need any code in this constructor, and you
// don't declare any other constructors, you can remove it
// entirely - the compiler will create it by default.
}
// Other methods here
}
Note how I've made all the fields private and non-static... you should almost certainly be creating an instance of EdgeNodeFactory rather than using static fields, and you should almost always make fields private.
Has been a while since I did any Java, but I believe that for loop should be inside a method or function of some description, rather than the class declaration.
I would imagine you mean that to be in a constructor.
I think what the for loops are meant to do is initialization of static array field. In this case you should put the code in a static initializer like this:
package com.pathfinding;
import java.util.ArrayList;
public class EdgeNodeFactory
{
static boolean[][] edgeMatrix = new boolean[100][100];
static {
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
edgeMatrix[i][j] = false;
}
}
}
static ArrayList<Node> nodes = new ArrayList<Node>();
static ArrayList<Edge> edges = new ArrayList<Edge>();
static int edgeCount = 0;
static int nodeCount = -1;
}
The code inside static { ... } at the class level is executed the first time the class is loaded (only once). This means it will be executed before any instances of the class are created and before any other code can access the class.
It remains debatable whether the fields should be static, but if you're sure they should, this is how you should initialize them.
I am trying to get the most occurring term frequencies for every particular document in Lucene index. I am trying to set the treshold of top occuring terms that I care about, maybe 20
However, I am getting the "no inclosing instance of type DisplayTermVectors is accessible" when calling Comparator...
So to this function I pass vector of every document and max top terms i would like to know
protected static Collection getTopTerms(TermFreqVector tfv, int maxTerms){
String[] terms = tfv.getTerms();
int[] tFreqs = tfv.getTermFrequencies();
List result = new ArrayList(terms.length);
for (int i = 0; i < tFreqs.length; i++) {
TermFrq tf = new TermFrq(terms[i], tFreqs[i]);
result.add(tf);
}
Collections.sort(result, new FreqComparator());
if(maxTerms < result.size()){
result = result.subList(0, maxTerms);
}
return result;
}
/*Class for objects to hold the term/freq pairs*/
static class TermFrq{
private String term;
private int freq;
public TermFrq(String term,int freq){
this.term = term;
this.freq = freq;
}
public String getTerm(){
return this.term;
}
public int getFreq(){
return this.freq;
}
}
/*Comparator to compare the objects by the frequency*/
class FreqComparator implements Comparator{
public int compare(Object pair1, Object pair2){
int f1 = ((TermFrq)pair1).getFreq();
int f2 = ((TermFrq)pair2).getFreq();
if(f1 > f2) return 1;
else if(f1 < f2) return -1;
else return 0;
}
}
Explanations and corrections i will very much appreciate, and also if someone else had experience with term frequency extraction and did it better way, I am opened to all suggestions!
Please help!!!! Thanx!
I think you'd need to make your TermFrq public static class.