473,657 Members | 2,531 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 1669
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
95879
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 references. void add(double *a1, double *a2, double *a3){ *a1 = *a2 + *a3; *a2 = *a1 + *a3; } In Java, one solution would be to create a class to represent a
25
2264
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
15988
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 script. Now I want to delete a line from a file. Line being the strings I got/saved to/from array of arrays.
1
4138
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 "CopyFromRecordset" (at least I don't think so) I got some info from the microsoft: http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q247412&ID=KB;EN-US;Q247412&LN=EN
15
2466
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; ofstream editFile; editFile.open (name, ios::out | ios::app);
13
6237
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 get me, which brings me to... I'm trying to create a program that gets a string from standard input and then manipulates it a little bit. It has to be a char array and not use string from the library. Here are my prototypes:
19
248149
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 this data is essential to any developer. This article is a basic tutorial on how to user HTML Forms, the most common method of data collection. Assumptions - Basic HTML knowledge. - Basic PHP knowledge. HTML Forms A common and simple way of...
7
2274
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( parameters ) { statements; }// end methtod and then the method call in the second class (the one created to use the methods of the first class) would be:
16
2532
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 at just comp.lang.c++. If one of these groups is too inappropriate, just take it off from where you send your replies.) Hi.
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
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 we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.