2016-08-29 10 views
0

私はしばらくの間、この課題に取り組んできましたが、私は立ち往生しています。目的は、数値が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(); 
    } 
} 
+0

と'マッパー'は、必要な機能を実装するための基本的なJavaのものを試してみることをお勧めします。 2つの変数を入れ替えることは、より多くを読んで課題を完了させるのに適しています。 Good Luck –

答えて

0

あなたがして、ほぼありました:現在、私が試した方法がコメントアウトされて

ここでは、コードです

numbers = Arrays.asList(words).stream().mapToInt(Integer::parseInt).toArray(); 

しかし、あなたが使用できるようにしますが、最初numbers配列を保存する必要があります後でwordsアレイを作成します。

int[] temp = numbers; 
numbers = Stream.of(words).mapToInt(Integer::parseInt).toArray(); 
words = IntStream.of(temp).boxed().map(Object::toString).toArray(String[]::new); 
0

それは次のように行うことができます。

public static void main(String[] args) 
     { 
//  The data 
     int[] numbers = {1, 2, 3, 4, 5}; 
     String[] words = {"6", "7", "8", "9", "10"}; 

    //  Create a new tempNumber array with word length 
      int[] tempNumber = new int[words.length]; 

    //  Set i=0 just for a shorter for loop 
      int i = 0; 
    //  Enter each string to the tempNumber array. 
      for(String s : words) 
      { 
       tempNumber[i++] = Integer.valueOf(s); 
      } 

    //  Set i to - so it can be reused. 
      i = 0; 

    //  Create a new tempNumber array with number length 
      String[] tempString = new String[numbers.length]; 
    //  Enter each int to the tempNumber array 
      for(int n : numbers) 
      { 
       tempString[i++] = String.valueOf(n); 
      } 

    //  Set numbers array to the one we created. 
      numbers = tempNumber; 
    //  Set string Array to the one we created; 
      words = tempString; 

    //  Output the result 
      System.out.println("The numbers array has: "+Arrays.toString(numbers)); 
      System.out.println("The string array has: "+Arrays.toString(words)); 

    //  Output~~~~~~~~~~~~~~~~ 
    //  The numbers array has: [6, 7, 8, 9, 10] 
    //  The string array has: [1, 2, 3, 4, 5] 
     } 
+0

私はこれを実装しようとしました。私が得た問題は、両方の配列がintになったということでした。許可されていない1-5は文字列になり、6-10はintになると考えられます。それ以外は正常に動作します – Werner

0

は、より簡単な方法は、おそらくありますが、私はこのようにそれをやった: `streams`を試す前に

public static void main(String[] args) 
{ 
    int[] numbers = {1,2,3,4,5}; 
    String[] words = {"6", "7", "8", "9", "10"}; 
    int[] mixed = new int[numbers.length + words.length]; 

    //loop through all numbers in the numbers[] array and put them in mixed 
    for (int i = 0; i < numbers.length;) 
    { 
     mixed[i] = numbers[i]; 
     i++; 

     //if i reached the length of the numbers[] array, do the following: 
     if (i >= numbers.length) 
     { 
      //continue using the i variable as index indicator while adding converting the string values into integers and adding them to the mixed[] array 
      for (int b = 0; b < numbers.length; b++) 
      { 
       mixed[i] = Integer.valueOf(words[b]); 
       i++; 
      } 
      break; 
     } 
    } 

    //print array 
    for (int x : mixed) 
    { 
     System.out.print(x + " | "); 
    } 
} 
} 
関連する問題