如何从 java 中的字符串数组中获取数组键
how to get array key from string array in java
我有一个具有重复值的字符串数组,然后我删除了重复项。现在,我需要从之前的结果中获取数组键并将其存储到新的 int 数组中。我不知道它在 Java 中如何工作,因为我搜索 java 不提供数组键。有人可以帮忙吗?
这是我的代码,例如:
static String[] temp2=new String[arraysimpanloop.size()];
static String[] temp2B4Remove=new String[temp2];
去除重复前的结果 temp2:
temp2 =[1, 1, 3, 3, 3, 3, 3, 3];
index of array=[0, 1, 2, 3, 4, 5, 6, 7];
删除重复后的结果 temp2:
temp2 =[1, 3];
index of array=[0, 2];
我的观点是,我需要像删除重复项之前那样获取数组键(数组索引)。 java可以吗?
尝试
int index = Arrays.asList(temp2).indexOf("3");
这是一种简单的方法:
String[] temp2 = new String[]{"1", "1", "3", "1", "3" };
List<String> values = new ArrayList<>();
List<Integer> indices = new ArrayList<>();
for( int i = 0; i < temp2.length; ++i ){
String s = temp2[i];
if( ! values.contains( s ) ){
values.add( s );
indices.add( i );
}
}
您现在可以从列表创建数组:
String[] uniq = values.toArray(new String[values.size()]);
Integer[] inds = indices.toArray(new Integer[indices.size()]);
那么,您想要 return 首次出现的不同项目的索引?
地图对于这些事情来说真的很酷,在这种情况下,您真正想要记住的只是每个不同元素第一次出现的时间。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DistinctValues {
public static List<Integer> distinctIndexes(String[] strings) {
// holds the mapping { string => index of first occurence of that string }
Map<String, Integer> firstOccurenceMap = new HashMap<String, Integer>();
// do a scan through all the items
for (int i = 0; i < strings.length; i++) {
// store the index position of a string only if you haven't encountered it before
firstOccurenceMap.putIfAbsent(strings[i], i);
}
return new ArrayList<Integer>(firstOccurenceMap.values());
}
public static void main(String[] args) {
String[] strings = new String[] { "1", "1", "3", "3", "3", "3", "3", "3" };
System.out.println(distinctIndexes(strings));
}
}
输出:
[0, 2]
我有一个具有重复值的字符串数组,然后我删除了重复项。现在,我需要从之前的结果中获取数组键并将其存储到新的 int 数组中。我不知道它在 Java 中如何工作,因为我搜索 java 不提供数组键。有人可以帮忙吗?
这是我的代码,例如:
static String[] temp2=new String[arraysimpanloop.size()];
static String[] temp2B4Remove=new String[temp2];
去除重复前的结果 temp2:
temp2 =[1, 1, 3, 3, 3, 3, 3, 3]; index of array=[0, 1, 2, 3, 4, 5, 6, 7];
删除重复后的结果 temp2:
temp2 =[1, 3]; index of array=[0, 2];
我的观点是,我需要像删除重复项之前那样获取数组键(数组索引)。 java可以吗?
尝试
int index = Arrays.asList(temp2).indexOf("3");
这是一种简单的方法:
String[] temp2 = new String[]{"1", "1", "3", "1", "3" };
List<String> values = new ArrayList<>();
List<Integer> indices = new ArrayList<>();
for( int i = 0; i < temp2.length; ++i ){
String s = temp2[i];
if( ! values.contains( s ) ){
values.add( s );
indices.add( i );
}
}
您现在可以从列表创建数组:
String[] uniq = values.toArray(new String[values.size()]);
Integer[] inds = indices.toArray(new Integer[indices.size()]);
那么,您想要 return 首次出现的不同项目的索引?
地图对于这些事情来说真的很酷,在这种情况下,您真正想要记住的只是每个不同元素第一次出现的时间。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DistinctValues {
public static List<Integer> distinctIndexes(String[] strings) {
// holds the mapping { string => index of first occurence of that string }
Map<String, Integer> firstOccurenceMap = new HashMap<String, Integer>();
// do a scan through all the items
for (int i = 0; i < strings.length; i++) {
// store the index position of a string only if you haven't encountered it before
firstOccurenceMap.putIfAbsent(strings[i], i);
}
return new ArrayList<Integer>(firstOccurenceMap.values());
}
public static void main(String[] args) {
String[] strings = new String[] { "1", "1", "3", "3", "3", "3", "3", "3" };
System.out.println(distinctIndexes(strings));
}
}
输出:
[0, 2]