在Java中切换int和字符串数组

时间:2016-08-29 10:14:02

标签: java arrays

我已经在这个任务工作了一段时间而且我被卡住了。目的是创建一个数字为1-5的int数组,然后用6-10创建一个字符串数组,然后将6-10放在int数组中,将1-5放在字符串数组中,然后再做一些东西它。我已经完成了大部分"东西"它(乘法,添加等),但我无法弄清楚如何相互切换两个数组。我尝试了一些我在stackoverflow上找到的方法,但我无法实现它们。目前我尝试的方法已被注释掉

以下是代码:

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();
   }
}

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

可以通过以下方式完成:

DECLARE
   var   VARCHAR2 (100);                                  -- Variable declared
BEGIN
   var := 'My name is jack';                              -- Assigning a string to the varibale

   DBMS_OUTPUT.put_line (var);                            -- Displaying it.


   SELECT 'My name is Mack' INTO var FROM DUAL;

   DBMS_OUTPUT.put_line (var);
END;

答案 2 :(得分:0)

可能有更简单的方法,但我这样做了:

    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 + " | ");
    }
}
}