• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2D-Array Computation Problems

 
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've created several methods in another class to perform certain matrix calculations. Do I need to have mutator method in this calculation class to define the column length of the matrix object? I understand why the following code is not working, but I was hoping to get a quick and easy workaround. I'm not sure how to initialize the size of the column for newMatrix within the method.

Then I will use this method to print the new matrix:



Is all of this overkill and should I just define all the methods within the same class to avoid these issues? Thanks.
 
Derek Smiths
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ans: matrixOne[0].length since all the column lengths will be the same.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Derek Smiths wrote:ans: matrixOne[0].length since all the column lengths will be the same.


Erm, isn't that making an assumption?

I suspect that would be fine for the types of matrix we did in school, but Java could quite happily sum two matrices of different sizes, and even different column lengths, so my answer to your question would be:
  int sumMatrix [][] = new int[Math.max(matrixOne.length, matrixTwo.length)][];
and then decide when you're actually working on a row what size it should be.

Winston
 
Derek Smiths
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are correct, it is probably better to implement the sum method in that way so it is not limited in its function. I was focusing on the assignment details which was to read two 3x3 matrices.
Jumping to conclusions since I had moved ahead to the product method of the program and tried to implement the same initalizer for sum. :P
 
Derek Smiths
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know you've already told me exactly what I should do, but when I try to figure out to implement that, I'm a little confused about how to define the column length after the first for loop.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't define column lengths after setting up the array. Not unless you want to change them. You have the .length field in every array object (It says so in the Java® Language Specification), so you can use that. You can loop up to the length field. You can of course change it
myArray[3] = new int[]{99, 98, 97, 96, 95, 94, 93, 92, 91, 90};
and (since 2D arrays are not really 2D at all but arrays of arrays), that will change the array length for one element.

The only problem you are likely to have is if you haven't initialised all the element arrays, in which case it will point to null and any attempt to get your hands on .length will produce a null pointer exception.
 
Derek Smiths
Ranch Hand
Posts: 119
Mac Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still getting an ArrayIOB Exception...

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the two arrays the same length? If you try to add the elements in a six‑element array to the elements in a five‑element array, you will have problems when you get to secondArray[5] because that is now out of the bounds of the 5‑element array.
 
reply
    Bookmark Topic Watch Topic
  • New Topic