简体   繁体   中英

increase value count of array

I have an array containing a bunch of int's.

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

now i want to increase the value count of this array by a value lets say 1.4 after this action the array wil look like this:

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

how would i go about doing this. i tried the following, but i got stuck on the logic and the code doesn't work. tips and suggestions are very welcome! see my try below:

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

I think i may have solved this problem. I had to put the array into an arraylist. i hope that is ok. but i have made it work i think tell me what you think..

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

Output:

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

It's unneccessary to have a method of the class, so I would make moreData static (and I would rename it to follow Java conventions); and I would pass in the scale -ing factor. Next, you might use a Map<Integer, Integer> (use a LinkedHashMap if you want a consistent order) to get the initial count of elements. Then you could use a flatMapToInt to generate an IntStream of the appropriate elements and counts with something like

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

Which I tested like

public static void main(String[] args) {
    int[] arr = { 17, 17, 18, 18, 18 };
    System.out.println(Arrays.toString(moreData(arr, 1.5)));
}

Getting

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

Hope this one will help you

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

Output:

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 

I have used LinkedHashMap to store the count of each integer and then formed a ArrayList of the increased count.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM