non fixed array

Hi,

mind to explain how i can create array in non-fixed value.let's say within for structure.currently im using arrayList but the value not declared as globally.so i cant call that value from other class.how can i declare the array as global so that i can call it from the another class?

//create table for each of the node

publicvoid createTable()

{

//to get i value

int i = currentNode.getiValue(sim.n);

//System.out.println("i" + i);

int k;

System.out.println();

for (int kValue=0; kValue<=i; kValue++)

{

int destList;

int size = 0;

Random r =new Random();

//add to arraylist for i value

ArrayList tmp =new ArrayList();

tmp.add(new Integer(kValue));

//current i value

int NodeI = (int)Math.round(Math.pow(2,kValue));

//i + 1

int nextNodeI = (int)Math.round(Math.pow(2,kValue + 1));

System.out.println("i" + tmp);

//arraylist for distance

ArrayList rows =new ArrayList();

for (int dest=NodeI; dest<nextNodeI; dest++)

{

//testing.arraylist for binary of distance

ArrayList binary =new ArrayList();

//add for distance

rows.add(new Integer(dest));

System.out.println("distance" + rows);

//add for binary

binary.add(String.valueOf(currentNode.getBits(dest)));

//System.out.println("binary" + binary);

}

System.out.println("array size" + rows.size());

//check if size of array > kbucket

if (rows.size() > sim.kBucket)

{

//remove old entries

removeDistance(rows);

System.out.println("empty rows" + rows);

for (int j=0; j<sim.kBucket; j++)

{

//list of new entries

destList = NodeI + r.nextInt((nextNodeI-1)- NodeI + 1);

//System.out.println("destList" + destList);

//add new entries

//rows.add(String.valueOf(destList));

rows.add(new Integer(destList));

System.out.println("new entries" + rows);

}

//sort

Collections.sort(rows);

System.out.println("sorted arrays" + rows);

}

}

}//create table

>

[3975 byte] By [gemini4607] at [2007-9-30 17:25:51]
# 1
You can declare the ArrayList static or you can put it in a Singleton. Both are ways to make things globally available.
UlrikaJ at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 2

Personally, I'd advise not thinking in terms of "global". It's a non-OOP way of looking at things. In OOP you have class/instance, and local/state (state being expressed by the fields of a class or instance). Working with those concepts design your app so the data you need is where you need it, encapsulated with the operations that need to use it.

If you're worrying about whether your array is global so lots of objects can have access to the raw data in that array, then you're not really doing OOP development.

IMHO.

paulcw at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 3
UlrikaJ,mind to show me what do u mean by static?u r referring to the function or the arrylist itself?if function,meaning i need to return the value,i dun want to do that but if you said on the arrylist itself,im not sure how to declare it.mind to show me?
gemini4607 at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 4
anyone got any idea how i can access my arraylist from diff class...& wat UljikaJ meant by declare the arraylist as static..how ?
gemini4607 at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 5
He meant to make the array static. "static" is a java keyword, and if you don't know what it means, the best advise is that you should hit the books and read about it. It's too fundamental to gloss over.
paulcw at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 6
Whoops. UJ is a she, isn't she?
paulcw at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 7
Why do you want other classes to access your arraylist? What will they be doing with it, and what are you hoping to accomplish?Do you know what a Singleton is?
paulcw at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 8

i declared my arrylist in RoutingTable class but i would like to call & print the result from the other class.why im doing this is because in simulator class i got 10nodes.each node should have their own table(meaning their own arraylist).later i will select randomly from the nodes & try to pull back all the table's values corresponding to the selected node(nodes[s] in simulator class)i wish to have it globally,so i can call it anywhere across my codes same as if i use normal arrays.

//Java extension packages

import java.util.*;

import java.io.*;

public class RoutingTable

{

SimulatorProp sim = new SimulatorProp();

Node currentNode;

static ArrayList tmp = null;

static ArrayList rows = null;

int testing;

public RoutingTable(Node currentNode)

{

this.currentNode = currentNode;

System.out.println("testing currentNode" + this.currentNode);

}

//create table for each of the node

public void createTable()

{

//to get i value

int i = currentNode.getiValue(sim.n);

//System.out.println("i" + i);

int k;

System.out.println();

for (int kValue=0; kValue<=i; kValue++)

{

int destList;

int size = 0;

Random r = new Random();

//add to arraylist for i value

ArrayList tmp = new ArrayList();

tmp.add(new Integer(kValue));

//current i value

int NodeI = (int)Math.round(Math.pow(2,kValue));

//i + 1

int nextNodeI = (int)Math.round(Math.pow(2,kValue + 1));

System.out.println("i" + tmp);

//arraylist for distance

ArrayList rows = new ArrayList();

for (int dest=NodeI; dest<nextNodeI; dest++)

{

//testing.arraylist for binary of distance

ArrayList binary = new ArrayList();

//add for distance

rows.add(new Integer(dest));

System.out.println("distance" + rows);

//add for binary

binary.add(String.valueOf(currentNode.getBits(dest)));

//System.out.println("binary" + binary);

}

testing = rows.size();

System.out.println("array size" + rows.size());

//check if size of array > kbucket

if (rows.size() > sim.kBucket)

{

//remove old entries

removeDistance(rows);

System.out.println("empty rows" + rows);

for (int j=0; j<sim.kBucket; j++)

{

//list of new entries

destList = NodeI + r.nextInt((nextNodeI-1)- NodeI + 1);

//System.out.println("destList" + destList);

//add new entries

//rows.add(String.valueOf(destList));

rows.add(new Integer(destList));

System.out.println("new entries" + rows);

}

//sort

Collections.sort(rows);

System.out.println("sorted arrays" + rows);

//testing(new ArrayList(rows));

}

testing(new ArrayList(rows));

}

}//create table

//remove entries,replace with the latest kbucket entries

public void removeDistance(Collection collection)

{

Iterator iterator = collection.iterator();

//while still have elements

while (iterator.hasNext())

if (iterator.next() != null)

iterator.remove();

}

//testing

public void testing(ArrayList list)

{

//int indx;

//for (int i = 0; i ><testing; i++)

//{

//int indx = Collections.binarySearch(list,rows.get(i));

System.out.println("testing" + list);

//}

}//testing

}//end class

//Java extension packages

import java.util.*;

import java.io.*;

//Class

public class Simulator

{//start class

//RoutingTable route;

SimulatorProp sim = null;

RoutingTable routingTable;

//constructor

public Simulator(SimulatorProp c)

{

sim = c;

}

//Main function

public static void main(String[] args) throws Exception

{//start main

Random random = new Random(0);

SimulatorProp prop = new SimulatorProp();

Simulator sim = new Simulator(prop);

sim.generateNodeId();

//start simulating

//randomly choose sender & destination

//int sender = 1 + (int) (Math.random()* sim.N);

//int dest = 1 + (int) (Math.random()* numNodes);

Node currentNode;

int s = (int) Math.round((Math.random()*(prop.n)));

int d = (int) Math.round((Math.random()*(prop.n)));

System.out.println("sender" + s);

System.out.println("receiver" + d);

System.out.println("sender" + s + " " + "id" + " " + prop.nodes[s].getNodeId());

System.out.println("receiver" + d + " " + "id" + " " + prop.nodes[d].getNodeId());

//create message to send

Message msg = new Message(prop.nodes[s],prop.nodes[d]);

//prop.nodes[s].routingtable.createTable();

//call simulate

}//end main

public void generateNodeId()

{

SortedSet nodeTree = new TreeSet();

//SortedSet node = new TreeSet();

long nodeId;

Random r = new Random(0);

System.out.println("Kademlia's Simulator");

System.out.println("\n");

for(int i=0; nodeTree.size()!=sim.n; i++)

{

nodeId = (long)Math.abs(sim.n*r.nextLong());

nodeTree.add(new Long (nodeId));

//System.out.println("Node Id" + " " + nodeTree);

}

//convert TreeSet into array

Iterator iterator = nodeTree.iterator();

sim.nodes = new Node[sim.n];

long t;

//Allocate node id into the array

for(int i=0; i><nodeTree.size(); i++)

{

t = ((Long)iterator.next()).longValue();

//pass value to node class

sim.nodes[i] = new Node(sim,t);

}

for (int i=0; i><sim.n; i++)

{

System.out.println("At Node" + ":" + " " + i + " " + "With Node Id" + ":" + " " + sim.nodes[i].getNodeId());

//generate table for each node

sim.nodes[i].routingtable.createTable();

}

}//end generateNode

}//end class

>

gemini4607 at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 9

is it possibly to do that in arraylist?from my testing,it only can be called from the same class & function even after i declared the arraylist as static.it will return null if i tried to call from new fuction that i created in the same class.should i convert it to normal structure of array to allow me to call & print from anywhere?.please advice

gemini4607 at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 10

> i declared my arrylist in RoutingTable class but i

> would like to call & print the result from the other

> class.

But, you shouldn't, you see. Any calling and printing on RoutingTable's data should be done by RoutingTable. What's the functionality you're trying to achieve by having other classes directly use RoutingTable's data?

> why im doing this is because in simulator class

> i got 10nodes.each node should have their own

> table(meaning their own arraylist).

That sounds like the opposite of having a single arraylist in RoutingTable. If each node should have their own ArrayList, why are you putting them all in RoutingTable?

paulcw at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 11

> later i will select randomly from the nodes & try to pull back all the

> table's values corresponding to the selected

> node(nodes[s] in simulator class)

Offhand I'd say there are two superior options:

1) associate these table values with the nodes they correspond to. That is, keep that data in the node.

2) Write a method in RoutingTable that takes a Node as an argument and returns whatever data is needed that corresponds to that node.

> i wish to have it

> globally,so i can call it anywhere across my codes

> same as if i use normal arrays.

This isn't C. Don't try to code C in Java. Keep your data with the functionality that uses it.

And by the way, there's no difference between arrays and ArrayLists in this context. (access permissions)

paulcw at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 12

Anyway, here's your problem. You're declaring "rows" and "tmp" as fields:

> public class RoutingTable

> {

> SimulatorProp sim = new SimulatorProp();

> Node currentNode;

> static ArrayList tmp = null;

> static ArrayList rows = null;

But then you declare them again as local variables when you set them:

> //add to arraylist for i value

> ArrayList tmp = new ArrayList();

> tmp.add(new Integer(kValue));

...

> //arraylist for distance

> ArrayList rows = new ArrayList();

So you never actually set the field. If you want to pursue this crummy design, just remove the "ArrayList" from before "tmp" and "rows" in the createTable method.

paulcw at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 13

meaning...

Issue 1

if i would like to keep my createtable function in routingTable,i need to do this?

1) associate these table values with the nodes they correspond to. That is, keep that data in the node.

2) Write a method in RoutingTable that takes a Node as an argument and returns whatever data is needed that corresponds to that node.

Issue2

otherwise,i need to move my createtable function to Node class in order to tied each of the node with correspond table?

To ur qeustion,y i put all my createtable in routingtable class is because im trying to this:

1.initialize nodes in nw

2.assignId to each node

3.create table for each of the node(routingtable class)

4.select sender & receiver

5.scan sender's table to reach destination.

my problem is at the no 4.after creating all the table for each of the nodes,i failed to call back the table that tied to the selected sender.i have no idea how to tied back the selected sender to the corresponding table.

[node]

//Java extension packages

import java.util.*;

import java.io.*;

public class RoutingTable

{

SimulatorProp sim = new SimulatorProp();

Node currentNode;

Simulator simulate;

static ArrayList tmp = null;

static ArrayList rows = null;

static Object distance[] = null;

public RoutingTable(Node currentNode)

{

this.currentNode = currentNode;

System.out.println("testing currentNode" + this.currentNode);

}

//create table for each of the node

public void createTable()

{

//to get i value

int i = currentNode.getiValue(sim.n);

//System.out.println("i" + i);

int k;

System.out.println();

for (int h=0 ; h<sim.tableIndex; h++)

{

for (int kValue=0; kValue><=i; kValue++)

{

int destList;

int size = 0;

Random r = new Random();

//add to arraylist for i value

ArrayList tmp = new ArrayList();

tmp.add(new Integer(kValue));

//current i value

int NodeI = (int)Math.round(Math.pow(2,kValue));

//i + 1

int nextNodeI = (int)Math.round(Math.pow(2,kValue + 1));

System.out.println("i" + tmp);

//arraylist for distance

ArrayList rows = new ArrayList();

for (int dest=NodeI; dest<nextNodeI; dest++)

{

//testing.arraylist for binary of distance

ArrayList binary = new ArrayList();

//add for distance

rows.add(new Integer(dest));

System.out.println("distance" + rows);

//add for binary

binary.add(String.valueOf(currentNode.getBits(dest)));

//System.out.println("binary" + binary);

}

System.out.println("array size" + rows.size());

//check if size of array > kbucket

if (rows.size() > sim.kBucket)

{

//remove old entries

removeDistance(rows);

System.out.println("empty rows" + rows);

for (int j=0; j<sim.kBucket; j++)

{

//list of new entries

destList = NodeI + r.nextInt((nextNodeI-1)- NodeI + 1);

//System.out.println("destList" + destList);

//add new entries

//rows.add(String.valueOf(destList));

rows.add(new Integer(destList));

System.out.println("new entries" + rows);

}

//sort

Collections.sort(rows);

System.out.println("sorted arrays" + rows);

Object distance[]=(Object[])rows.toArray();//converting into an object array.

for(int j=0;j<distance.length;j++)

{

System.out.println("Array else"+distance[j]);//converting object into string

}

}//close if

else

{

Object distance[]=(Object[])rows.toArray();//converting into an object array.

for(int j=0;j<distance.length;j++)

{

System.out.println("Array if "+distance[j]);//converting object into string

}

}//close else

}//kvalue

}//table index

}//create table

public void returnArray()

{

for(int p=0;p<distance.length;p++)

{

System.out.println("i dunno"+distance[p]);//converting object into string

}

}

//remove entries,replace with the latest kbucket entries

public void removeDistance(Collection collection)

{

Iterator iterator = collection.iterator();

//while still have elements

while (iterator.hasNext())

if (iterator.next() != null)

iterator.remove();

}

}//end class

//Java extension packages

import java.util.*;

import java.io.*;

//Class

public class Simulator

{//start class

//RoutingTable route;

SimulatorProp sim = null;

RoutingTable routingTable;

static int hops = 0;

//constructor

public Simulator(SimulatorProp c)

{

sim = c;

}

//Main function

public static void main(String[] args) throws Exception

{//start main

RoutingTable routingTable;

Random random = new Random(0);

SimulatorProp prop = new SimulatorProp();

Simulator sim = new Simulator(prop);

sim.generateNodeId();

//start simulating

//randomly choose sender & destination

int s = (int) Math.round((Math.random()*(prop.n - 1)));

int d = (int) Math.round((Math.random()*(prop.n - 1)));

System.out.println("sender" + s);

System.out.println("receiver" + d);

System.out.println("sender" + s + " " + "id" + " " + prop.nodes[s].getNodeId());

System.out.println("receiver" + d + " " + "id" + " " + prop.nodes[d].getNodeId());

//create message to send

Message msg = new Message(prop.nodes[s],prop.nodes[d]);

//call simulate

if (simulate(msg) == false);

hops++;

}//end main

public static boolean simulate(Message msg)

{

RoutingTable route;

Node currentNode;

currentNode = msg.source;

currentNode.showTable();

return true;

}

public void generateNodeId()

{

SortedSet nodeTree = new TreeSet();

//SortedSet node = new TreeSet();

long nodeId;

Random r = new Random(0);

System.out.println("Kademlia's Simulator");

System.out.println("\n");

for(int i=0; nodeTree.size()!=sim.n; i++)

{

nodeId = (long)Math.abs(sim.n*r.nextLong());

nodeTree.add(new Long (nodeId));

//System.out.println("Node Id" + " " + nodeTree);

}

//convert TreeSet into array

Iterator iterator = nodeTree.iterator();

sim.nodes = new Node[sim.n];

long t;

//Allocate node id into the array

for(int i=0; i><nodeTree.size(); i++)

{

t = ((Long)iterator.next()).longValue();

//pass value to node class

sim.nodes[i] = new Node(sim,t);

}

for (int i=0; i><sim.n; i++)

{

System.out.println("At Node" + ":" + " " + i + " " + "With Node Id" + ":" + " " + sim.nodes[i].getNodeId());

//generate table for each node

sim.nodes[i].routingTable.createTable();

}

}//end generateNode

}//end class

[/node]

>

gemini4607 at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 14

> if i would like to keep my createtable function in

> routingTable,i need to do this?

If you went with option 2 (have a method to retrieve data based on Node, rather than letting any object anywhere have access to all the data), yes you would or could do this.

> otherwise,i need to move my createtable function to

> Node class in order to tied each of the node with

> correspond table?

Well you wouldn't have to tie anything explicitly. The data would be with the stuff that needs it, so that would be the tying. You'd probably have some kind of per-node create-table functionality, but I don't know if you'd even need a table.

paulcw at 2007-7-6 13:48:44 > top of Java-index,Java Essentials,Java Programming...
# 15

> To ur qeustion,y i put all my createtable in

> routingtable class is because im trying to this:

>

> 1.initialize nodes in nw

"nw"?

> 2.assignId to each node

The nodes could create their own ID, or the object instantiating them could.

> 3.create table for each of the node(routingtable class)

What's this table for and why?

> 4.select sender & receiver

How do senders and receivers relate to a given node?

> 5.scan sender's table to reach destination.

> my problem is at the no 4.after creating all the table

> for each of the nodes,i failed to call back the table

> that tied to the selected sender.i have no idea how to

> tied back the selected sender to the corresponding

> table.

I don't understand what you're saying. I realize that English may not be your first language, but if you used proper capitalization, avoided run-on sentences, used correct spelling, and stopped using so many abbreviations, you'd communicate more effectively.

paulcwa at 2007-7-19 22:34:31 > top of Java-index,Java Essentials,Java Programming...