我已经在这个任务上工作了一段时间,但我被卡住了。其目的是生成一个1-5的整型数组,然后生成一个6-10的字符串数组,然后将6-10放入整型数组,将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();
}
}发布于 2016-08-29 18:23:11
你几乎已经做到了:
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);发布于 2016-08-29 18:31:20
可以通过以下方式完成:
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]
}发布于 2016-08-29 18:49:50
可能有更简单的方法,但我是这样做的:
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 + " | ");
}
}
}https://stackoverflow.com/questions/39203750
复制相似问题