• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector Collection  RSS feed

 
V Bose
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aim : Create a 2 dimensional array data structure @ runtime.
Premise : New to Java!
What I tried :
int i=0;
Vector Correlation;
Correlation = new Vector();
while (i < NRows)
{
Correlation.addElement(new Vector());
i++;
}
((Vector)Correlation.get(0)).add("10"); //Correlation[0][0]=10
((Vector)Correlation.get(0)).add("5"); //Correlation[0][1]=5

Question 1 : Is there another(less painful) way I can simulate a flexible, m-dimensional array @ runtime.
Question 2 : Am I using the right way to access the Vector of Vectors in Correlation
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're doing is basically sound. To make it less painful, you would encapsulate the whole thing in a class. This class could use Vector in its implementation, or it might even just use arrays.
The class (call it Matrix) could have methods like

Then you could say

Finally: note that there is a very strongly respected coding standard that says Java variable names should begin with a lowercase letter; writing "Correlation" with an uppercase letter makes your code harder to read for those of us who are used to reading Java code.
 
V Bose
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOps. Soory about the capitalization of variables...
If you notice, in my earlier piece of code...
((Vector)correlation.get(0)).add("10"); //correlation[0][0]=10
adds the element "10" to the vector of vector.
But how do I modify this value "10" now ?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using my Matrix class, or using your original code? In the Matrix class, you just call set(0, 0, "20"). In your approach, you'd have to do something like
((Vector)correlation.get(0)).setElementAt("20", 0);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!