私はしばらくの間、この課題に取り組んできましたが、私は立ち往生しています。目的は、数値が1-5のint配列を作成し、次に6-10の文字列配列を作成し、6-10をint配列に入れ、1-5を文字列配列に入れ、その後いくつかのものを実行することですそれに。私はそれに "stuff"(掛け算、追加など)のほとんどを行ってきましたが、どうやって2つの配列をお互いに切り替えるのか分かりません。私はstackoverflowで見つけたいくつかのメソッドを試しましたが、実装できませんでした。Javaのint配列と文字列配列の切り替え
import java.util.*;
import java.io.*;
public class Rebel
{
public static void main (String[] args)
{
int[] numbers = {1,2,3,4,5};
String[] words = {"6", "7", "8", "9", "10"};
System.out.println(numbers.getClass().getName()); // test data type before converting
System.out.println(words.getClass().getName()); // test data type before converting
for(int i = 0; i < numbers.length; i++) // prints out int array
{
System.out.println(numbers[i]);
}
for(int j = 0; j < words.length; j++) // prints out string array
{
System.out.println(words[j]);
}
/* Switching the arrays
//java.util.Arrays.toString(numbers[]); // converts int to string
// numbers = Arrays.asList(words).stream().mapToInt(Integer::parseInt).toArray(); // convert string to int
// int [] tempNum = Arrays.asList(words.split(",")).stream().map(String::trim).mapToInt(Integer::parseInt).toArray();
//int [] tempNum = Arrays.asList(words.split(",")).stream().mapToInt(Integer::parseInt).toArray();
*/
System.out.println("There are " + numbers.length + " elements in numbers array");
System.out.println("There are " + words.length + " elements in words array");
System.out.println(java.util.Arrays.toString(numbers));
System.out.println(java.util.Arrays.toString(words));
for(int num: numbers)
{
num = num*4;
System.out.println(num);
}
for (String word: words)
{
System.out.println(stringMultiply(word, 3)); // s = word, and n = 3;
}
System.out.println(numbers.getClass().getName()); // test data type after converting
System.out.println(words.getClass().getName()); // test data type after converting
}
public static String stringMultiply(String s, int n) /// "multiply" string
{
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++)
{
sb.append(s);
}
return sb.toString();
}
}
と'マッパー'は、必要な機能を実装するための基本的なJavaのものを試してみることをお勧めします。 2つの変数を入れ替えることは、より多くを読んで課題を完了させるのに適しています。 Good Luck –