簡體   English   中英

增加數組的數值

[英]increase value count of array

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

我想我可能已經解決了這個問題。 我不得不將數組放入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

moreData類的方法,因此我將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]

希望這對你有幫助

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 

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM