Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java字符数组打印索引_Java_Arrays - Fatal编程技术网

Java字符数组打印索引

Java字符数组打印索引,java,arrays,Java,Arrays,字符数组[]是“x,a,x,c,x,b,x,a,x,x,b,x,x,x,x” 键数组[]是“a,b,c” 预期返回数组:“1,5,3” 目标是打印与键数组匹配的字符数组的索引。例如,在这种情况下,程序必须打印“1、5、3”。它只计算它匹配的第一个索引 另一个例子是 字符数组[]是“q,h,e,h,w,e,r,t,l,y,l,l,o” 键数组[]是“h,e,l,l,o” 预期返回数组:“1,2,8,10,12” 到目前为止,我所尝试的是 int index = 0; for(int i = 0;

字符数组[]
是“x,a,x,c,x,b,x,a,x,x,b,x,x,x,x”

键数组[]
是“a,b,c”

预期返回数组:“1,5,3”

目标是打印与
键数组
匹配的
字符数组
的索引。例如,在这种情况下,程序必须打印“1、5、3”。它只计算它匹配的第一个索引

另一个例子是

字符数组[]
是“q,h,e,h,w,e,r,t,l,y,l,l,o”

键数组[]
是“h,e,l,l,o”

预期返回数组:“1,2,8,10,12”

到目前为止,我所尝试的是

int index = 0;
for(int i = 0; i < key_array.length; i++)
{
    isFound = false;
    for(int k = index + 1; k < char_array.length && isFound == false; k++) 
    {
        if(char_array[i] == key_array[k])
        {
            index = k;
            num[j] = index;
            isFound = true;
        }
    }
}
int索引=0;
对于(int i=0;i
这样,我的第二个关于“hello”的例子就行了,但是我的第一个关于“abc”的例子就行不通了

我用
index+1
启动了
k
,但我想我必须将其从0更改为
char\u array.length

有人能帮我解释一下这个逻辑吗请试试这个

for(int i=0;i<key_array.length;i++)
{
    int pos=new String(char_array).indexOf(key_array[i]);

    char_array[pos]='0'                          //considering there is no numeric character in char_array  

    collection.push(pos);                        //collection is a java Collection framework's object
}
for(inti=0;i似乎起到了作用

static int[] foo(char[] char_array, char[] key_array) {
    // copy the original so we can modify to avoid repeats
    char[] copy = new char[char_array.length];
    System.arraycopy(char_array, 0, copy, 0, char_array.length);
    int[] result = new int[key_array.length];
    boolean found = true;
    for(int i = 0; found && i < key_array.length; i++) {
        found = false;
        for(int j = 0; j < copy.length; j++) {
            if (copy[j] == key_array[i]) {
                copy[j] = 0;
                result[i] = j;
                found = true;
                break;
            }
        }
    }
    if (found) {
        return result;
    }
    return null;
}

public static void main(String[] args) {
    System.out.println(Arrays.toString(foo("xaxcxbxaxxbxxxx".toCharArray(), "abc".toCharArray())));
    System.out.println(Arrays.toString(foo("qhehwertlyllo".toCharArray(), "hello".toCharArray())));
}
static int[]foo(char[]char\u数组,char[]key\u数组){
//复制原件,以便我们可以修改以避免重复
char[]copy=新字符[char_array.length];
System.arraycopy(字符数组,0,复制,0,字符数组,长度);
int[]结果=新的int[key_array.length];
布尔值=真;
对于(int i=0;找到和&i
这只适用于第二个示例,因为字符是按顺序出现的

但是,您需要根据之前是否搜索过该字符的字符串(在找到该字符之后开始),来决定开始搜索的索引

例如


我认为这会起作用(O(n)):

char[]char\u数组={'q','h','e','h','w','e','r','t','l','y','l','o'};
char[]key_数组={'h','e','l','l','o'};
Map charpositions=new HashMap();
for(int i=0;i
您好,这应该是个好办法

public class Main
{

  private static final char[] liste1 = {'x', 'a', 'x', 'c', 'x', 'b', 'x', 'a', 'x', 'x' ,'b' ,'x' ,'x', 'x', 'x'};
  private static final char[] liste2 = {'q', 'h', 'e', 'h', 'w', 'e', 'r', 't', 'l', 'y', 'l', 'l', 'o'};
  private static final char[] key1 = {'a', 'b', 'c'};
  private static final char[] key2 = {'h', 'e', 'l', 'l', 'o'};


  private static void lookupIndexOfChar(char c, char[] list, List<Integer> result){
    for(int i = 0; i < list.length; i++){
      if(list[i] == c){
        if(notInResult(i, result)){
          result.add(i);
          break;
        }
      }
    }
  }

  private static boolean notInResult(int i, List<Integer> result)
  {
    return !(result == null || result.contains(i));
  }

  public static void main(String[] args){
    List<Integer> result = new ArrayList<Integer>();
    for (char c : key1)
    {
      lookupIndexOfChar(c, liste1, result);
    }
    for (Integer integer : result)
    {
      System.out.print(integer);
    }
    System.out.println(" ");
    result.clear();
    for (char c : key2)
    {
      lookupIndexOfChar(c, liste2, result);
    }
    for (Integer integer : result)
    {
      System.out.print(integer);
    }
  }

}
公共类主
{
私有静态最终字符[]列表1={'x','a','x','c','x','b','x','a','x','b','x','x','x','x','x'};
私有静态最终字符[]列表2={'q','h','e','h','w','e','r','t','l','y','l','l','o'};
私有静态final char[]key1={'a','b','c'};
私有静态final char[]key2={'h','e','l','l','o'};
私有静态void lookupIndexOfChar(char c,char[]列表,列表结果){
for(int i=0;i
提示:在“key”数组上循环,对于每个字符C,在另一个数组上循环,当您找到C时,打印索引并
中断
,然后重复为什么“h,e,l,l,o”版本不输出“1,2,8,8,12”?您需要采取措施重新初始化索引0@Tibrogargan因为8已经计数了。所以不应该再计数。@user7但是当我将索引重新初始化为0时,它会像上面的hello示例一样再次计数索引,它不应该打印1 2 8 8 12,它应该是1 2 8 10 12为什么不呢?
char\u array
永远不会更新。它是p在loop@cricket_007所以他可以使用indexOf。棘手。太糟糕了,输出是错误的,这将是一个很好的解决方案。所以,这没有考虑重复字符不能有相同的数字。请参阅“hello”示例我以为您要求将整个语句保留在循环之外。将字符串创建移到循环之外实际上只是编译器无论如何都会进行的优化。真正的问题是解决方案会产生错误的结果
// find list of indices by char
Map<Character, ?> map = IntStream.range(0, char_array.length).boxed().collect(Collectors.groupingBy(i -> char_array[i]));

for (Map.Entry e : map.entrySet()) {
    // replace values with iterator over index lists
    e.setValue(((List)e.getValue()).iterator());
}

for (int i = 0; i < key_array.length; i++) {
    Iterator<Integer> iterator = (Iterator<Integer>) map.get(key_array[i]);
    num[i] = (iterator == null || !iterator.hasNext() ? -1 : iterator.next());
}
char[] char_array = {'q', 'h', 'e', 'h', 'w', 'e', 'r', 't', 'l', 'y', 'l', 'l', 'o'};
char[] key_array = {'h', 'e', 'l', 'l', 'o'};

Map<Character, Queue<Integer>> charPossitions = new HashMap<Character, Queue<Integer>>();
for(int i = 0; i < char_array.length; i++){
    if(charPossitions.get(char_array[i]) == null){           
       Queue<Integer> possitionsQueue=new LinkedList<Integer>();
       possitionsQueue.add(i);
       charPossitions.put(char_array[i], possitionsQueue);
    }else { 
       charPossitions.get(char_array[i]).add(i);
    }                                           
}
for(char key : key_array){
    System.out.println(key + "/" + charPossitions.get(key).poll());
}
public class Main
{

  private static final char[] liste1 = {'x', 'a', 'x', 'c', 'x', 'b', 'x', 'a', 'x', 'x' ,'b' ,'x' ,'x', 'x', 'x'};
  private static final char[] liste2 = {'q', 'h', 'e', 'h', 'w', 'e', 'r', 't', 'l', 'y', 'l', 'l', 'o'};
  private static final char[] key1 = {'a', 'b', 'c'};
  private static final char[] key2 = {'h', 'e', 'l', 'l', 'o'};


  private static void lookupIndexOfChar(char c, char[] list, List<Integer> result){
    for(int i = 0; i < list.length; i++){
      if(list[i] == c){
        if(notInResult(i, result)){
          result.add(i);
          break;
        }
      }
    }
  }

  private static boolean notInResult(int i, List<Integer> result)
  {
    return !(result == null || result.contains(i));
  }

  public static void main(String[] args){
    List<Integer> result = new ArrayList<Integer>();
    for (char c : key1)
    {
      lookupIndexOfChar(c, liste1, result);
    }
    for (Integer integer : result)
    {
      System.out.print(integer);
    }
    System.out.println(" ");
    result.clear();
    for (char c : key2)
    {
      lookupIndexOfChar(c, liste2, result);
    }
    for (Integer integer : result)
    {
      System.out.print(integer);
    }
  }

}