• Post Reply Bookmark Topic Watch Topic
  • New Topic

2d arrays  RSS feed

 
alex ols
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i have to create a 2d array class that has 2 constructors.
first constructor: Matrix(int[][] array) Constructs a Matrix from a two dimensional array;the dimensions as well as the values of this Matrix will be the same as the
dimensions and values of the two dimensional array.

second constructor: Matrix(int size1, int size2) Constructs a size1 by size2 Matrix of zeroes.

This is my code:

The main problem is in the methods i need to create afterwards, the methods should have a return type of Matrix, the problem is when i try to return a Matrix after making some changes to the array i get an error that says: "incompatible types: int[][] cannot be converted to Matrix." i don't understand why i get this error and what should i do to fix it. would be happy for some help or even a reference to a source like a guide or something because all i found was not very helpful. Thanks in advance.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alex ols wrote:
The main problem is in the methods i need to create afterwards, the methods should have a return type of Matrix, the problem is when i try to return a Matrix after making some changes to the array i get an error that says: "incompatible types: int[][] cannot be converted to Matrix." i don't understand why i get this error and what should i do to fix it. would be happy for some help or even a reference to a source like a guide or something because all i found was not very helpful.


You tell us all about the requirements for the constructors, and even the code for the constructors ... however ...

Besides telling us the error message for the method, you don't give us any details regarding the methods. What are you trying to do? Can you show us the methods? etc.

Henry
 
alex ols
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, the only method i tried writing is a method that should fill an array with the complements of each integer element of the original array, the original array consists of elements ranging from 0 to 255(i can assume that this really is the range that has been put in each element so i don't need anything that makes sure the elements contain the specified range) for example if an element holds the integer 250 then after using the method on the original array i should get a copy of the original array with this element being the integer 5(that should be the case for all other elements).
this is the code, it is probably incorrect:
  
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, from the method, you only have one array variable in scope, and that is the private instance variable. So, "this.array" and "array" variable references are the same variable.  This means that line 5 creates a new array with default values, line 8 (assuming that it is even in array range) doesn't do anything, as all the elements have default values.

Second, your method is declared to return a Matrix object. An int array is not a Matrix object.... you should either change the declaration, or change what is returned.

And third, I am not sure why you need to assume that the array is 255 is size. You are not making that assumption anywhere else in your code, ie. it will work with any size array, so why do you need to add this limitation with this method?

Henry
 
alex ols
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's an assignment and one of the rules is that i can assume the elements to be in that specific range so i wont have to write a code that checks if the user puts the correct numbers in the array.
I changed the declaration of the array to Matrix[][] array. when creating such a method, do i need to create a new array? or should i use other.array? also should i delete the lines were i declare the size1 and size2 ints?.
i read some guides and the 2d array creation they showed was pretty simple , the whole constructor creation is what i don't understand.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alex ols wrote:I changed the declaration of the array to Matrix[][] array.

That is most likely wrong.  A matrix of integers can be represented as an int[][].  To declare something as Matrix[][] means that you have a matrix of Matrix objects. Now you've married "The Matrix" with "Inception" and that's probably going to result in "The Big Bang," only it won't be so funny for you.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me you're getting yourself all confused between the high level concept of a Matrix and the implementation details for representing one with arrays of int.  It might do you good to step back away from the keyboard and work out some of these high-level ideas and low-level implementation details on paper first.  Only when you fully understand your problem will you be able to write the proper code for the solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!