Is it possible to instantiate two int arrays without using a return type?

This particular problem I'm working on is indicated as such:

ConcatArrays (int [] listA, int [] listB, int [] listC) without return type. 1. The method passes the formal parameters of the listA and listB array, and then returns the concatenated listC array. 2. The first part of list C contains elements that are the same as the corresponding elements of list A, and the second part contains the same elements as listB. The length of listC is listA.length + listB.length.

This is what I have, and obviously it won’t work. Is it possible to do this without returning the type / removing list c as the third parameter?

public static void ConcatArray(int[] listA, int[] listB, int[] listC) { int aLen = listA.length ; int bLen = listB.length; int cLen = listC[aLen + bLen]; } 

Edit: I appreciate all the answers at all, I voted for everyone. This was homework, and after re-reading, I believe that the return type was not an error due to what he said in question 1. Basically, to return list c using the listB and ListA parameters. Previously, there were several other typos that he corrected. He also gave us array1 and array 2 for our test class with values, but without array 3.

+5
source share
3 answers

After creating a Java array, you cannot resize it. Your code does not work because the reference to the array is passed by value, and Java does not pass by reference.

Returning an array is an idiomatic way to resolve this issue in Java:

 public static int[] ConcatArray(int[] listA, int[] listB) { int[] res = new int[listA.length + listB.length]; ... return res; } 

You can fake passing by reference through a mutable type that has a field of the desired type, for example:

 class IntArrayRef { int[] array; } 

Now you can write your void method as follows:

 public static void ConcatArray(int[] listA, int[] listB, IntArrayRef res) { res.array = new int[listA.length + listB.length]; ... } 

However, if you handle this, you can return an array.

+5
source

The trick is that the contents of the array are not what is passed to the method. The contents of the array can be read and written, but writing one changes the original. Java does not create a new copy for you unless you use it yourself. This means that you can transfer information back just by using the picture in what was transmitted to you. This is great as long as you document the behavior, so someone using this method is not surprised when their data is goodbye.

 public static void main(String[] args) { int[] a = {0, 1}; int[] b = {2, 3}; int[] c = new int[a.length + b.length]; concatArray(a, b, c); for (int i : c) { System.out.println(i); } } /** Concatenates prefix and suffix into result which must be as large as both */ public static void concatArray(int[] prefix, int[] suffix, int[] result) { int prefixLength = prefix.length; int suffixLength = suffix.length; if( result.length < prefixLength + suffixLength) { throw new IllegalArgumentException(); } for (int i = 0; i< prefixLength; i++) { result[i] = prefix[i]; } for (int i = 0; i< prefixLength; i++) { result[i+prefixLength] = suffix[i]; } } 

exit:

 0 1 2 3 

The same trick works with objects until they are immutable. If someone hands you a bean, you can play setters with him, and if you shouldn't, cause all kinds of chaos.

This is why setting the identifier of an array or object as final does not guarantee immutability. Any user can still call configuration methods (if they exist on the object) or create new assignments in the array. All final ones are handcuffs for an object or an array for an identifier. He does not block the portfolio. Getting rid of setters blocks the portfolio.

What we call immutable. Lines are unchanged and therefore this trick will not work with Strings.

When you go to the method as if you made the final identifier. You cannot modify an array or object without the calling context losing information about what you are playing with. If you stick to the same array or object, you can play with it as much as it is allowed, and the call context will see everything that you have done.

Infact, whenever you return anything other than a primitive, what you really return is a new place and says, "Look what I did here." This trick works simply by sticking to previously known places.

+1
source

In fact, no one uses Simple Array because you cannot resize it or add / remove. Most programmers use the ArrayList class because it is more flexible and powerful.

- create an instance of the arraylist class

  java.util.ArrayList<String> cities = new java.util.ArrayList<String>(); 

- to add a variable to your arraylist

  cities.add("Alexandria"); 

- to get a variable position in your arraylist

  cities.indexOf("Cairo")); 

- to remove the first variable from your arraylist (it is zero, like a simple array)

  cities.remove(0); 

- to get the length of your arraylist

  cities.size(); 

take a look at the Java Documentation for more information on the ArrayList class http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

0
source

Source: https://habr.com/ru/post/1205524/


All Articles