• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

using set and get methods

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

hor do i use accessor and mutator methods with an array
does this looks ok

private int arr[];

public void setArr()
{
arr = new int[1];
}

public int[] getArr()
{
return arr;
}

thanks for any help
basi
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite right. It really depends on what you wanna do with the array. Do you wanna return an element value from the specific index? Or a fixed location everytime?

When you uses a setter method, where do you wanna set the new data into? A fixed position or an arbitrary position?

Usually, not always, setter methods have to take in a parameter, usually the data you wishes to assign to a variable. And a getter method has no arguments while returning the value that you're interested in obtaining.

Hope this helps.
 
basia fish
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would that be more appropriate

private int arr[];

public void setArr(int bArr[])
{
for (int i=0; i<aArr.length-1; i++)
arr[i] = aArr[i];
}

public int[] getArr()
{
if (arr==null)
arr=new int[10];
return arr;
}


thanks for help
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Cheng stated, it depends on your application. Whether you are sorting or adding or indexing...etc the array then you would have different accessor and mutator methods. It looks like the methods that you have created are just putting numbers in the index and then you are trying to return the numbers. Remember that int is a primitive type and when you try to print out this list of numbers that you are adding in you will just get a memory location for the entire array. If you want to see the numbers then just change the get method to SOP the arr[i]. I would suggest something like this:



HTH

Gabe
[ June 16, 2004: Message edited by: Gabriel White ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic