Java 增加数组的值计数

Java 增加数组的值计数,java,arrays,mapping,Java,Arrays,Mapping,我有一个数组,包含一堆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,

我有一个数组,包含一堆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;
}
int[]更多数据(int[]VAL){
ArrayList updated_data=新的ArrayList();
ArrayList temp=新的ArrayList();
整数计数=17;
对于(int i=0;i
不需要类的方法,因此我会使
更多数据
静态
(我会将其重命名为遵循Java约定);我将通过
比例
-ing因子。接下来,您可以使用
Map
(如果您想要一致的顺序,可以使用
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();
}
得到

[17, 17, 17, 18, 18, 18, 18, 18]

希望这个能帮助你

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);
    }
}

我想我可能已经解决了这个问题。我必须将数组放入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());
}

我使用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);
publicstaticvoidmain(字符串[]args){
int[]屏幕ID={17,17,13,13,13,12,11,11,11,10,10,9,9,9,8,7,7,5,5,4,4,3,3,3,2,2,1};
System.out.println(屏幕长度);
int initial=1;
LinkedHashMap lhm=新LinkedHashMap();

对于(int i=0;我能说得更具体些吗?将值增加
1.4
是什么意思?这些值的变化到底如何?我所说的1.4是指我想将所有唯一值的计数增加1.4。因此,如果数组包含2x 17,那么新数组应该包含3x 17,所有唯一值也是如此。希望这能让它更精确清楚!我想我会得到2.2,我会把它增加到3,因为这更适合我的需要。我还是不太明白。还要注意的是,除非你@提到我们,否则我们不会收到关于你回复的通知。我不知道@KevinWorkman,我会尽量说得更清楚。假设一个数组包含两个相同的值,让我们说这些值是17。在一个新数组中,我想复制旧值,但将这些值的计数增加1.5,这样新数组将包含三个值17。而不是两个值为17的旧数组。你知道在处理过程中应该导入哪些java库吗?代码现在无法编译。。谢谢回答@Elliott Frisch
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
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);