Java中的String [] S,在其中添加值,现在再创建2个数组

在 Java 中创建一个大小为 x 的 String[] S 数组,添加一些值 init ,现在再创建两个数组 even[] 和 odd[],偶数数组应包含值 String S[] 和偶数索引号和 odd[]将包含具有奇数索引号的字符串 S[] 的值


package trying;


import java.util.Scanner;


public class ArrayTest {


    public static void main(String[] args) {

        int val;

        int evcounter = 0;

        int odcounter = 0;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter total no of elements ");

        val = sc.nextInt();

        System.out.println("Enter values ");

        String[] n = new String[val];

        for (int i = 0; i < n.length; i++) {

            n[i] = sc.next();


            if (i % 2 == 0) {

                evcounter++;

            } else {

                odcounter++;

            }


            if (evcounter + odcounter == val) {

                String[] eve = new String[evcounter];

                String[] odd = new String[odcounter];

                System.out.println("******Please Help AFTER THIS**********");


            }

        }

    }

}

预期输出:


Enter total no of elements 

3

Enter Values

HI

hello

Bye

Even array:[HI,Bye]

ODD array :[hello]


SMILET
浏览 194回答 3
3回答

杨__羊羊

你需要这样的东西:public static void main(String[] args) {&nbsp; &nbsp; int val;&nbsp; &nbsp; int evcounter = 0;&nbsp; &nbsp; int odcounter = 0;&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter total no of elements ");&nbsp; &nbsp; val = sc.nextInt();&nbsp; &nbsp; System.out.println("Enter values ");&nbsp; &nbsp; String[] n = new String[val];&nbsp; &nbsp; String[] eve = new String[(int) Math.ceil(val/2D)];&nbsp; &nbsp; String[] odd = new String[val/2];&nbsp; &nbsp; for (int i = 0; i < n.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; n[i] = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; if (i % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eve[evcounter] = n[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; evcounter++;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; odd[odcounter] = n[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; odcounter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("Even array: " + Arrays.toString(eve));&nbsp; &nbsp; System.out.println("Odd array: " + Arrays.toString(odd));}

郎朗坤

如果您使用的是 1.8+,那么最好使用易于理解且优雅的函数式风格。import java.util.Arrays;import java.util.function.IntPredicate;import java.util.stream.Collectors;import java.util.stream.IntStream;public class MyTestCase {&nbsp; @Test&nbsp; public void evenOddStringArrayPrinting() {&nbsp; &nbsp; String[] src = new String[] {"HI", "Hello", "Bye"};&nbsp; &nbsp; final String[] evenArray = getArrayFrom(src, i -> i % 2 == 0);&nbsp; &nbsp; final String[] oddArray = getArrayFrom(src, i -> i % 2 == 1);&nbsp; &nbsp; System.out.println(Arrays.toString(evenArray));&nbsp; &nbsp; System.out.println(Arrays.toString(oddArray));&nbsp; &nbsp; Assert.assertEquals(2, evenArray.length);&nbsp; &nbsp; Assert.assertEquals(1, oddArray.length);&nbsp; }&nbsp; private String[] getSome(final String[] src, final IntPredicate predicate) {&nbsp; &nbsp; return IntStream.range(0, src.length)&nbsp; &nbsp; &nbsp; &nbsp; .filter(predicate)&nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(i -> src[i])&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList())&nbsp; &nbsp; &nbsp; &nbsp; .toArray(new String[] {});&nbsp; }}

富国沪深

public static void main(String[] args) {&nbsp; &nbsp; int evenCounter = 0;&nbsp; &nbsp; int oddCounter = 0;&nbsp; &nbsp; Scanner reader = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter total no of elements ");&nbsp; &nbsp; int num = reader.nextInt();&nbsp; &nbsp; String[] n = new String[num];&nbsp; &nbsp; System.out.println("Enter values:");&nbsp; &nbsp; for (int i=0;i<n.length;i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; n[i]=reader.next();&nbsp; &nbsp; &nbsp; &nbsp; if (i % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; evenCounter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oddCounter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; String[] even = new String[evenCounter];&nbsp; &nbsp; String[] odd = new String[oddCounter];&nbsp; &nbsp;for (int z=0;z<n.length;z++)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;if (z%2==0)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int j=0;j<even.length;j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (even[j]==null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;even[j]=n[z];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;else if (z%2==1)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int j=0;j<odd.length;j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (odd[j]==null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;odd[j]=n[z];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}这个想法是遍历原始 String 数组并检查它的索引是否为偶数/奇数(就像您在代码中所做的那样检查有多少赔率/偶数)。如果索引是偶数,则遍历 evens 数组并检查数组的第一个空单元格(在 java 中,当您生成字符串数组时,您会得到一个充满空值的数组,因此您搜索第一个空值并将其替换为值来自原始数组)。赔率也一样。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java