【问题标题】:increase value count of array增加数组的值计数
【发布时间】:2018-04-24 20:53:03
【问题描述】:

我有一个包含一堆 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;
}

【问题讨论】:

  • 你能说得更具体些吗?将值增加1.4 是什么意思?这些值究竟是如何变化的?
  • 我所说的 1.4 的意思是我想将所有唯一值的计数增加 1.4。因此,如果数组包含 2x 17,则新数组应包含 3x 17,因此对于所有唯一值。希望这能让它更清楚!
  • 如果我会得到 2.2,我会将其增加到 3,因为这更适合我的需要
  • 我还是不太明白。另请注意,除非您@提及我们,否则我们不会收到有关您回复的通知。
  • 我不知道@KevinWorkman,我会尽量说得更清楚。假设一个数组包含两个相同的值假设这些值是 17。在一个新数组中,我想复制旧值但将这些值的计数增加 1.5,因此新数组将包含三个值 17。而不是旧的两个值为 17 的数组。

标签: java arrays mapping


【解决方案1】:

拥有类的方法是不必要的,所以我将 moreData static (我将重命名它以遵循 Java 约定);我会传入scale-ing 因子。接下来,您可以使用Map&lt;Integer, Integer&gt;(如果您想要一致的顺序,请使用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]

【讨论】:

  • 您知道在处理过程中应该导入哪些 java 库吗?代码现在无法编译.. 感谢@Elliott Frisch 的回答
【解决方案2】:

希望这篇文章对你有帮助

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】:

    我想我可能已经解决了这个问题。我不得不将数组放入数组列表中。我希望没关系。但我已经成功了,我想告诉我你的想法..

    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
    

    【讨论】:

      【解决方案4】:

      我已经使用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);
      

      【讨论】:

        猜你喜欢
        • 2017-11-04
        • 1970-01-01
        • 1970-01-01
        • 2010-12-21
        • 1970-01-01
        • 1970-01-01
        • 2018-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多