Solved

# Matrix value assingment

Posted on 2011-05-08
245 Views
Hi all

I need to assing a value to an array instance..
See attached code for problem definition..
``````Class Cells
public Cells(int x, int y, int V_size){
this.x = x;
this.y = y;
W_vector = new double[V_size];
geneLs = new LinkedList<Gene>(); // not much consideration need

}
Cells[] map; //I created a instance of Cells class
map = new Cells[Q];  // I initialize it to ;; int Q = 6

// I assing some values to the vectors
for(int i = 0; i < map.length; i++)
{
double f1 = Math.random();
int kk = (int)(f1*3.0);
for(int j = 0; j < N; j++)
{
double f2 = Math.random();
int ll = (int) (f2*3.0);

map[i] = new Cells(M, N, vectorMySize);
map[i].x = kk; // assing the value of kk to position x: This is the source of the problem
map[i].y = ll; // assign the value of ll to position y:: This is the source of the problem
map[i].set_weight_vector(vsize); // this is not important
}
}

// assign the value of return map
this.retunMap = map;

I need to be able to print out the value of the map with this code
System.out.println("Coordinates for a 3 by 2 map:");
for(int i=0; i<map.length; i++){

System.out.println("Node " + i + ": (" + map[i].get_x() + " " + map[i].get_y() + ")");
}

The code for get method is as follows
public int get_x(){
return this.x;
}

public int get_y(){
return this.y;
}

// My result at the moment
// Note the Node value
Node 0 0 Node 1 1
weight vector: 0.00 0.00 0.00
Genes:

Node 0 1 Node 1 1
weight vector: 0.00 0.00 0.00
Genes:

//Expected output
Node 0 1
weight vector: 0.00 0.00 0.00
Genes:
``````
0
Question by:ZURINET

LVL 47

Expert Comment

Well, the statement of your problem does not seem quite clear.
You show your result at the moment,
but your code isn't even close to be compiled.

Perhaps it would be better if you post a real code with comments
in the form of real comments
In this code say from the very beginning you
have this piece:

Class Cells
public Cells(int x, int y, int V_size){
this.x = x;
this.y = y;
W_vector = new double[V_size];
geneLs = new LinkedList<Gene>(); // not much consideration need

}

Even when I corrected the case for say Class
there is such problem that youj first need to declare instatnce variables x and y
and then have them assigned in the constructor:
say
``````public class Cells {
int x;
int y;

public Cells(int x, int y, int V_Size) {
this.x=x;
this.y=y;
double []W_vector = new double[V_size];
...
}
``````

No surprise beow you have
``````map[i].x , map[i].y
``````
,

where you write "this is the source of the problem" -
it complains because Cells do not have
declared fields x and y

In short, there are many issues in this code ,
so "see attahed code for problem definition"
does not work very well.
If that would be the code close
to ready, then it would have been possible to state the question this way,
but not in this case.

0

Author Comment

Hoi
Given that I have the variables in class Cells declared
however the values being stored in map coordinates are not uniqe..

To keep the question short, I am looking for a way to store
1D (dimenstion) array of M*N nodes; with the Nodes storing as follows

Node 0: (0 0)
weight vector: 0.00 0.00 0.00
Genes

Node 1: (0 1)
weight vector: 0.00 0.00 0.00
Genes
Node 2: (1 0)
weight vector: 0.00 0.00 0.00
Genes
Node 3: (1 1)
weight vector: 0.00 0.00 0.00
Genes
Node 4: (2 0)
weight vector: 0.00 0.00 0.00
Genes
Node 5: (2 1)

Node 0 0
weight vector: 0.00 0.00 0.00
Genes:

``````map[i].y = ll;
and map[i].x = kk;  are not uniqe..
``````
0

LVL 47

Expert Comment

You can use ArrayList and use your Cell class which will conatin x and y
ArrayList al = new ArrayList();
Cell c = new Cell(2,1...);<---whatever else you need in the constructor

Cell c = new (0,1...);

and so on, you can have aloop adding them

Then you should have methods which retrive x and y from

and then you can go through your arraylist
retirive each element and print your x and y or do with them whatever you
want.
In this case x any  do not need to be uique, you can store any number of cells with equal x and y
0

LVL 47

Expert Comment

You of couse don't need to declare variable "c" twice , so in my
sketch above second time I emasnt you'll say just

c =newCell(0,1...)

not

Cell c = ...
0

LVL 92

Accepted Solution

you can use something like the following:

Cell[] cells = new Cell[N*M];

for (int j=0; j<M; j++) {
for (int i=0; i<N; i++) {
cells[j * N + i] = new Cell(i, j);
}
}

// then to get cell (x,y)

Cell cell = cells[y*N + x];
0

Author Comment

Hi objects..

I would love to send you some swiss chocolates :-)

You are the Guru!
Thanks a lot..
0

LVL 92

Expert Comment

I love chocolate, and the swiss are pretty good at making it :)

Glad I could help. Have just hired another Java tutor so should have more time available now to help with your questions.
0

## Featured Post

### Suggested Solutions

fix34  challenge 9 70
Convert ArrayList of custom objects to JSON 4 44
copyEndy  challenge 15 39
scoresSpecial  challenge 13 34
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.