A multi-dimensional array is also a 1-dimensional array: int[a][b][c] is really just int[a*b*c], so the problem boils down to, how do you provide access safely? Simply like this:
public class Foo {
private int[] array;
public Foo(int[] array) {
this.array = Arrays.copyOf(array, array.length);
}
/** @return a copy of the array */
public int[] getArray() {
return Arrays.copyOf(array, array.length);
}
}
That's it.
Callers have a safe copy of the array and can use it in the full normal way arrays are used. No need for delegator methods.
Answer from Bohemian on Stack OverflowA multi-dimensional array is also a 1-dimensional array: int[a][b][c] is really just int[a*b*c], so the problem boils down to, how do you provide access safely? Simply like this:
public class Foo {
private int[] array;
public Foo(int[] array) {
this.array = Arrays.copyOf(array, array.length);
}
/** @return a copy of the array */
public int[] getArray() {
return Arrays.copyOf(array, array.length);
}
}
That's it.
Callers have a safe copy of the array and can use it in the full normal way arrays are used. No need for delegator methods.
What do you think ArrayList does, or Vector before it?
I think the better question is why, at this point, are you exposing that Foo is backed by an array at all? If you're trying to encapsulate it, you don't need to have accessors and setters all over the place. If you're just trying to create a class wrapper around the array, then I would suggest you have an interface, call it IntList or something, and make Foo a concrete implementation that's backed by the list.
Setter Getter Arrays Java - Stack Overflow
Java array getters and setters.. help? | AnandTech Forums: Technology, Hardware, Software, and Deals
Using a getter to set the size of an array
java - Do I have to make getter setters for each element of an array? - Stack Overflow
In Java, array is a reference type, which means that the value of an array expression is a reference to the actual array.
The return value of getA() is, therefore, a reference to the private array inside your object. This breaks encapsulation: you give access to your object's internals.
You can avoid this by either returning a reference to a copy of your internal array, or by providing a different API which only returns individual elements, say a method getA(int index).
f.get(A) returns a reference to an array. You can access that array the way you access any array, and assign values to its elements with f.get(A)[i]=... (though it makes more sense to store the returned array in a variable, which would let you access that array multiple times, without having to call f.get(A) each time).
You can't, however, assign anything f.get(A) via f.get(A)=.., since a method call is not a valid left side of an assignment operator. For all you know, a call to f.get(A) may generate a new array that is not referred to by a member of the foo class, so assigning f.get(A)= new int[5]; would make no sense, since there would be no variable in which to store the new array.
The same explanation applies to f.getB() = 5;.
You are using Array of Lecture objects and overwriting the same array with two different array references. Hence, it is not working. Use the below code:
public class Student {
private Lecture[] lecture;
public void setStudentLecture(Lecture[] lecture) {
this.lecture = lecture;
}
public Lecture[] getStudentLecture() {
return lecture;
}
public static void main(String[] args) {
Student student = new Student();
Lecture[] lectures = new Lecture[3];
lectures[0] = new Lecture("Physics");
lectures[1] = new Lecture("Mathematics");
lectures[2] = new Lecture("Chemistry");
student.setStudentLecture(lectures);
Lecture[] lectures1 = student.getStudentLecture();
for (int i = 0; i <lectures1.length; ++i) {
System.out.println(lectures1[i].getName());
}
}
}
public class Lecture {
private String name;
public Lecture(String name) {
this.name = name;
}
public String getName(){
return name;
}
}
As you setter is also array, you can create the Array of Lecture and set it to Student.
sample:-
Student student = new Student();
Lecture lecture = new Lecture();
Lecture lecture1 = new Lecture();
Lecture[] lectureArr = new Lecture[]{lecture, lecture1};
student.setStudentLecture(lectureArr);
And also you have studentLecture as array, then why you want to assign different array twice, you can combine both array and assign it.
Hi, I'm a bit of a beginner to Java but am really enjoying it, it's my first OOP language.
I am doing an assignment where I have to create an array (can't be an array list) that's size is dependant on what the user allocated at the beginning. To do this, I've tried to use a setter and getter method. Unfortunately, when I'm testing my code, it looks like I haven't done it correctly because the array size is always 0. I've identified that it's taking the original declaration variable size rather than the size allocated by the setter.
private int num;
public BikeSystem(int num){
setNum(num);
}
//getters`
public int getNum(){
return num;
}
//setters
public void setNum(int num){
this.num = num;
}
//Make ArrayList to store bikes
Bike [] bikeList = new Bike [getNum()];
If anyone could share their knowledge with me I would be super grateful.
Thank you so much.
ArrayList is a mutable container class, though, so you don't actually need a setter at all: simply have callers call getStringList() and then mutate the ArrayList themselves:
public final class DataHolder {
private final ArrayList<String> stringList = new ArrayList<>();
public ArrayList<String> getStringList() {
return stringList;
}
}
public static void main(String[] args) {
DataHolder data = new DataHolder();
data.getStringList().add("foo");
}
On the other hand, the more common requirement is to prevent callers from being able to mutate internal data structures, so that your class can actually enforce its invariants on its data. ArrayList is always mutable, so if you need to return an ArrayList but you don't want your private state to be modified, you'll need to copy:
public ArrayList<String> getStringList() {
return new ArrayList<String>(stringList);
}
Alternatively, if possible, it's better to widen the return type and then return some other implementation that your class can use to enforce its invariants. For example, if you don't want people to modify your ArrayList using a getter method, you could widen the type from ArrayList<String> to List<String> and use an unmodifiable implementation, for example:
public List<String> getStringList() {
return Collections.unmodifiableList(stringList);
}
On the other hand, for the setter, it depends on how you want to handle the values. Normally a setFoo method replaces the contents of foo, which you can of course do:
public void setStringList(ArrayList<String> stringList) {
this.stringList = stringList;
}
But most likely you want to also widen the type you will accept. For example, you could accept any Collection instead of just an ArrayList:
public void setStringList(Collection<String> strings) {
this.stringList = new ArrayList<>(strings);
}
It may be more useful, however, to instead expose methods to mutate the underlying list in other ways. For example, perhaps you simply want to support adding new items to the list:
public void addString(String string) {
this.stringList.add(string);
}
public void addStrings(Collection<String> strings) {
this.stringList.addAll(strings);
}
If you do decide to expose a setter that replaces the value, you probably want to first check it for correctness. Since your class initializes the ArrayList in an instance initializer, most likely you don't expect it will ever be null. So you should throw an exception if it is:
public void setStringList(List<String> stringList) {
if (stringList == null) {
throw new NullPointerException("stringList must not be null");
}
this.stringList = stringList;
}
Java 7 added a new Objects.requireNonNull method for exactly this purpose.
To set the value, you would take an ArrayList<String> passed as a parameter, and you would simply set the ArrayList to the parameter.
Also, note the use of void rather than the ArrayList<String> shown in your question. Commonly, a setter method does not return anything.
public void setStringList(ArrayList<String> stringList)
{
this.stringList = stringList;
}