Return of Java arrays

Source: Internet
Author: User

Suppose we want to write a method now, and we don't want it to just return something, but rather to return a series of things. At this point, languages like C and C + + complicate the problem because we cannot return an array, only to return a pointer to an array. This is very troublesome, because it is difficult to control the "time" of the array, it is easy to cause memory "vulnerabilities" appear.
Java uses a similar approach, but we can "return an array". Of course, the actual return at this point is still the pointer to the array. But in Java, we never have to worry about the availability of that array-it will automatically exist as long as it is needed. And the garbage collector will automatically clear it when we're done.
As an example, consider how to return an array of strings:
 

: Icecream.java//returning arrays from methods public class Icecream {static string[] Flav = {"Chocolate", " Strawberry "," Vanilla Fudge Swirl "," Mint Chip "," Mocha Almond Fudge "," Rum Raisin "," Praline Cream "," Mud Pie
  " 
  }; Static string[] Flavorset (int n) {//Force it to be positive & within bounds:n = Math.Abs (n)% (flav.length
    + 1);
    String[] results = new String[n];
    Int[] Picks = new Int[n];
    for (int i = 0; i < picks.length i++) Picks[i] =-1; for (int i = 0; i < picks.length i++) {Retry:while (true) {int t = (int) (Math.random ()
        * flav.length);
        for (int j = 0; J < i; j +) if (picks[j] = = t) continue retry;
        Picks[i] = t;
        Results[i] = flav[t];
      Break
  } return results; public static void Main (string[] args) {for (int i = 0; i < i++) {System.out.println ("Flavo
      RSet ("+ i +") = "); string[FL = Flavorset (flav.length);
    for (int j = 0; J < Fl.length; J + +) System.out.println ("T" + fl[j]); }
  }
} ///:~

The Flavorset () method creates a string array named results. The size of the array is n--the exact numeric value depends on the arguments we pass to the method. It then randomly picks some "spices" (flavor) from the array Flav and put them into results and eventually returns to results. Returning an array is no different from returning any other object--the final return is a handle. The question is not important as to whether the array was created in Flavorset () or somewhere else, since it is only a handle returned anyway. Once our operation is complete, the garbage collector will automatically take care of the array cleanup work. And as long as we need an array, it will be obediently awaiting dispatch.
On the other hand, note that when Flavorset () randomly picks spices, it needs to make sure that a random selection that has previously occurred does not reappear. In order to achieve this goal, it uses an infinite while loop, constantly making random selections until it finds an element that is not in the picks array (and, of course, it can be compared to check if the random selection has occurred in the results array, but the string comparison is less efficient). If successful, add the element, interrupt the loop (break), and then find the next one (I will increment the value). But if T is an array that has already appeared in the picks, use the tabbed continue to jump back two levels, forcing a new T to be selected. This process can be clearly seen with a debugger.
Main () can display 20 complete sets of spices, so we see Flavorset () Each time select spices in a random order. The easiest way to realize this is to redirect the output to a file and view the contents of the file directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.