我有一个包含一堆int的数组。
int[] screen_ids = {17, 17, 13, 13, 13, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 8, 7, 7, 7, 5, 5, 4, 4, 3, 3, 3, 2, 2, 1};
现在我想在此操作之后将数组的值计数增加一个值1.4
,数组将如下所示:
int[] screen_ids = {17, 17, 17, 13, 13, 13, 13, 13, 12, 12, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8, 7, 7, 7, 7, 7, 5, 5, 5, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 1, 1};
我将如何做到这一点。我尝试了下面这个,但是我坚持逻辑,代码不起作用。非常欢迎提示和建议!看下面我的尝试:
int[] more_data(int[] vals){
ArrayList<Integer> updated_data = new ArrayList<Integer>();
ArrayList<Integer> temp = new ArrayList<Integer>();
int count = 17;
for(int i = 0; i < vals.length; i++){
if(vals[i] == count){
temp.add(vals[i]);
}else{
temp.size() * 1.4;
for(int j = 0; j < temp.size(); j++){
updated_data.add(temp.get(j));
}
}
}
return updated_data;
}
答案 0 :(得分:1)
我想我可能已经解决了这个问题。我不得不将阵列放入arraylist。我希望没关系。但我已经让它发挥作用我想告诉我你的想法......
public static void processData(int[] vals, int lookingFor, double incSize, List list) {
double count = 0;
for (int i : vals) {
if (i == lookingFor) {
System.out.println(i);
count++;
}
}
System.out.println("count: " + count);
incSize = count * incSize;
System.out.println("incBy: " + incSize);
int rounded = (int) Math.round(incSize);
System.out.println("rounded: " + rounded);
for (int i = 0; i < rounded; i++) {
list.add(lookingFor);
}
System.out.println("Result: ");
for (Object i : list) {
System.out.println(i);
}
}
public static void main(String[] args) {
List<Integer> x = new ArrayList<>();
int[] screen_ids = {17, 17, 13, 13, 13,
12, 11, 11, 11, 10, 10, 10, 9, 9, 9,
9, 8, 7, 7, 7, 5, 5, 4, 4, 3, 3, 3, 2, 2, 1};
for (int i : screen_ids) {
x.add(i);
}
processData(screen_ids, 17, 1.4, x);
System.out.println(" x length: " + x.size());
}
输出:
17 17 count: 2.0 incBy: 2.8 rounded: 3 Result: 17 17 13 13 13 12 11 11 11 10 10 10 9 9 9 9 8 7 7 7 5 5 4 4 3 3 3 2 2 1 17 17 17 x length: 33
答案 1 :(得分:0)
拥有类的方法是不必要的,所以我会创建moreData
static
(我会将其重命名为遵循Java约定);我会传递scale
因素。接下来,您可以使用Map<Integer, Integer>
(如果您想要一致的订单,请使用LinkedHashMap
)来获取元素的初始计数。然后,您可以使用flatMapToInt
生成IntStream
相应的元素,并使用
static int[] moreData(int[] vals, double scale) {
Map<Integer, Integer> countMap = new LinkedHashMap<>();
IntStream.of(vals).forEachOrdered(i -> countMap.put(i,
1 + countMap.getOrDefault(i, 0)));
return countMap.entrySet().stream().flatMapToInt(entry ->
IntStream.generate(() -> entry.getKey()).limit(
Math.round(entry.getValue() * scale))).toArray();
}
我测试的是
public static void main(String[] args) {
int[] arr = { 17, 17, 18, 18, 18 };
System.out.println(Arrays.toString(moreData(arr, 1.5)));
}
获取
[17, 17, 17, 18, 18, 18, 18, 18]
答案 2 :(得分:-1)
希望这个人会帮助你
public class test {
private static final List<Integer> ids = Arrays.asList(17, 17, 13, 13, 13, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 8, 7, 7, 7, 5, 5, 4, 4, 3, 3, 3, 2, 2, 1);
private static final List<Integer> newIds = new ArrayList<>();
public static void main(String[] args) {
int lastChecked = 0;
for (int id : ids)
{
newIds.add(id);
if (lastChecked != id)
checkForIncrease(id);
lastChecked = id;
}
ids.forEach(num -> System.out.print(num + " "));
System.out.println();
newIds.forEach(num -> System.out.print(num + " "));
}
private static void checkForIncrease(int val)
{
final double mult = 1.4;
int found = 0;
for (int num : ids)
if (num == val)
found++;
final double multResult = found * mult;
if (multResult - found > 0.5 && found > 1)
for (int i = 0; i < multResult - found; i++)
newIds.add(val);
}
}
输出:
17 17 13 13 13 12 11 11 11 10 10 10 9 9 9 9 8 7 7 7 5 5 4 4 3 3 3 2 2 1
17 17 17 13 13 13 13 13 12 12 11 11 11 11 11 10 10 10 10 10 9 9 9 9 9 9 8 8 7 7 7 7 7 5 5 5 4 4 4 3 3 3 3 3 2 2 2 1 1
答案 3 :(得分:-1)
我使用LinkedHashMap来存储每个整数的计数,然后形成一个增加计数的ArrayList。
public static void main(String[] args) {
int[] screen_ids = {17, 17, 13, 13, 13, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 8, 7, 7, 7, 5, 5, 4, 4, 3, 3, 3, 2, 2, 1};
System.out.println(screen_ids.length);
int initial=1;
LinkedHashMap<Integer,Integer> lhm=new LinkedHashMap<Integer,Integer>();
for(int i=0;i<screen_ids.length;i++)
{
if(!lhm.containsKey(screen_ids[i]))
{
lhm.put(screen_ids[i], initial);
}
else
{
lhm.put(screen_ids[i],lhm.get(screen_ids[i])+1);
}
}
List<Integer> screen_ids1 = new ArrayList<Integer>();
for(Map.Entry m:lhm.entrySet()){
int new_count=(int)(((int)m.getValue())*1.4+1);
lhm.put((int)m.getKey(), new_count);
System.out.println(m.getKey()+" "+m.getValue());
for(int i=0;i<(int)m.getValue();i++)
{
screen_ids1.add((int)m.getKey());
}
}
System.out.println(screen_ids1);