473,503 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a put() method in arrays

3 New Member
Hey,
I'm pretty new to Java and am having some trouble writing a put() method for one of my programs. The basic idea of it is to have a matrix, made by a multi-dimensional array, then to create the method with three arguements, the row, the column and the object to place into this array.

To declare the array, and the two objects I have this
Expand|Select|Wrap|Line Numbers
  1.     int [] [] matrixArray = new int [5] [5];
  2.  
  3.  
  4.     Object a = "a";
  5.     Object b = "b";
  6.  
Then I have a method declaration that looks like this (with x being row, y being column)

Expand|Select|Wrap|Line Numbers
  1. public void put(int x, int y, Object object) {
  2.  
  3.   =matrixArray(x, y, object); // This clearly doesn't work as it hasn't even been fully filled out, but is along the lines of what I'm thinking!
  4.  
  5. }
  6.  
I understand that I would need to have something like,
matrixArray.put (1, 3, a)
to insert the object, but I am really lost at where to start with writing this method. Any help would be appreciated!

Thanks
Apr 2 '08 #1
5 1654
r035198x
13,262 MVP
Hey,
I'm pretty new to Java and am having some trouble writing a put() method for one of my programs. The basic idea of it is to have a matrix, made by a multi-dimensional array, then to create the method with three arguements, the row, the column and the object to place into this array.

To declare the array, and the two objects I have this
Expand|Select|Wrap|Line Numbers
  1.     int [] [] matrixArray = new int [5] [5];
  2.  
  3.  
  4.     Object a = "a";
  5.     Object b = "b";
  6.  
Then I have a method declaration that looks like this (with x being row, y being column)

Expand|Select|Wrap|Line Numbers
  1. public void put(int x, int y, Object object) {
  2.  
  3.   =matrixArray(x, y, object); // This clearly doesn't work as it hasn't even been fully filled out, but is along the lines of what I'm thinking!
  4.  
  5. }
  6.  
I understand that I would need to have something like,
matrixArray.put (1, 3, a)
to insert the object, but I am really lost at where to start with writing this method. Any help would be appreciated!

Thanks
1.) An int[] can only store integers.
2.) To store the integer 5 an int[] called array at the second position you use
Expand|Select|Wrap|Line Numbers
  1. array[1] = 5;
Apr 2 '08 #2
JosAH
11,448 Recognized Expert MVP
Your matrixArray object needs access to some real matrix; why not make that
matrix a member of the MatrixArray class?

Expand|Select|Wrap|Line Numbers
  1. public class MatrixArray {
  2.    private Object[][] m;
  3.    public MatrixArray(int n, int m) {
  4.       m= new Object[n][m];
  5.    }
  6.    Object get(int i, int j) { return m[i][j]; }
  7.    ...
  8.    // your put(Object o, int i, int j) method here
  9. }
  10.  
This is most of the proposed class: the constructor gets the dimensions of the
matrix and builds a private matrix. I already did the get() method for you. You
do the put() method.

kind regards,

Jos

ps. Maybe the Decorator pattern article I wrote a while ago is a nice read.
Apr 2 '08 #3
mmmtacos
3 New Member
1.) An int[] can only store integers.
2.) To store the integer 5 an int[] called array at the second position you use
Expand|Select|Wrap|Line Numbers
  1. array[1] = 5;
Hey,
Thanks for the reply [=

I understand this, but how would I integrate the put() method into that line of code?
Apr 2 '08 #4
r035198x
13,262 MVP
Hey,
Thanks for the reply [=

I understand this, but how would I integrate the put() method into that line of code?
You'd need a class for it (all methods must belong to a class in Java) and that's when the class suggested by Jos above comes in.
Apr 2 '08 #5
mmmtacos
3 New Member
Ohhhh that makes alot more sense now haha
Sorry JosAH I missed your post before

So using this I can do something more like, m.put() instead, which would make alot more sense!

Thanks for your help guys. I'll have a read of the article because my next task will be using generics!
Apr 2 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
95874
by: S Manohar | last post by:
I need to pass a 'reference' to a double value to a function which changes that value. Each time the function is called, it needs to change different sets of values. In C I'd simply pass...
25
2237
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
2
15970
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
1
4124
by: gssstuff | last post by:
Hi I have a need to send data from Access to an existing Excel template that I am using for reporting purposes. The nature of the data in the Access data table does not lend itself to a simple...
15
2450
by: rEvolution27 | last post by:
I'm a c++ newbie here, trying out some stuff and when I try to compile this: void create() { char name; cout << "Creating a new timetable /n Please type a name for this timetable"; cin >name;...
13
6221
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
19
247994
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
7
2266
sammyboy78
by: sammyboy78 | last post by:
I was wondering about the format of calling methods using arrays. I know that to call a method when not using arrays it would be formatted in the first class like: public void methodName(...
16
2508
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.