如何将一个对象传递给一个方法,然后再传递给另一个方法

How to pass an object to a method and then to an other method

我目前正在处理的程序应该采用一个字符串数组,混合字符串数组的内容(例如 word1 word2 word3 / 重新排序为 word3 word1 word2)并在终端中显示输出。我已经尝试了 4 种不同的 print 变体,但其中 none 似乎有效。有没有我缺少的功能?

import java.util.Random;
Random Random = new Random();

void setup(){

  String[] text3={"Word1", "Word2", "Word3", "Word4", "Word5", "Word6"};
  printArray(scramble(text3));
}

void scramble(String[] str) {                 
  for (int i = 0; i < str.length; i++) {
    int rd0 = Random.nextInt(i+1);
    int rd1 = Random.nextInt(i+1);
    String temp = str[rd0]; 
    str[rd0] = str[rd1]; 
    str[rd1] = temp; 
  }
}

printArray 接受数组作为参数。

你实际上给了它scramble的结果,也就是void

调用 scramble然后 调用 printArray,将要打印的数组传递给它。

所以,只需将数组传递给第一个方法,然后再传递给第二个方法:

scramble(text3);
printArray(text3);