Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Vector Collection

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic