使int数组与java字符串数组并行

Soo*_*oon 1 java arrays

以下代码将字符串数组'one'创建为[c,a,t,a,n,d,d,o,g].现在我想创建一个int数组'two',其中每个'a'的位置是3,所有其他位置都填充为5

int two= {5, 3, 5, 3, 5, 5, 5, 5, 5}
Run Code Online (Sandbox Code Playgroud)

但是代码将每个元素赋予等于5,因此它打印为

5 5 5 5 5 5 5 5 5 :
Run Code Online (Sandbox Code Playgroud)

我用的代码从这里开始:

import com.google.common.collect.ObjectArrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;

public class StringInt {


public static void main(String[] args) {

      String str= "cat and dog";
      String[] word = str.split("\\s"); 
      String[] one = new String[0];
      for(int i=0; i<word.length; i++){
           one = ArrayUtils.addAll(one, word[i].split("(?!^)"));
      } 

        System.out.println("One : " + Arrays.toString(one));

        int[] b = new int[one.length];

        for(int j=0; j<one.length; j++){
            if(one[j]=="a"){
                b[j]=3;
             } else {
                b[j]=5;
             }
            System.out.print(b[j]+" ");

          }

        }
   }
Run Code Online (Sandbox Code Playgroud)

作为编程和java的新手我需要帮助来纠正这段代码以获得所需的输出:

5  3  5  3  5  5  5  5  5
Run Code Online (Sandbox Code Playgroud)

Tim*_*m B 6

您正在使用==.equals()比较字符串.

使用one[j].equals("a").