满足特定条件时交换元素到arraylist

时间:2019-08-26 01:27:13

标签: java arrays sorting arraylist

我有一个arraylist = [1,2,2,0,0,0,0,0,0,0] or [0,1,1,3,2,2,0,0,0,0,0,0,0,3,0]

我希望它是= [1,0,0,0,0,2,2,0,0,0], or [1,1,0,0,0,2,2,0,0,0,3,3,0,0,0]

算法不仅交换集合,还需要一个条件。

因此,数组的总元素总是可以除以最大值本身

例如。 array[]=[1,2,2,0,0,0,0,0,0,0]包含2个最大值,因此元素总数可以除以2,因此每个条件有5个元素。

然后,应根据数组中的最小值到最大值对数组进行排序。 之后,将零值作为其余5个元素

现在应该是[1,0,0,0,0,2,2,0,0,0]

已经实施:

List<Integer> list = new ArrayList<>(); // contains 1,2,0,0,0,0,0,0,0,0
List<Integer> newList = new ArrayList<>();

for (int i = 0; i < list.size(); i++){
   if( i == list.size() - 1){
      newList.add(list.get(i));
      break;
   }
   if(list.get(i).equals(0) && list.get(i+1).equals(0)){
      break;
   } else if(!list.get(i).equals(0) && list.get(i+1).equals(0)){
      newList.add(list.get(i));
      break;
   }

   if(!list.get(i).equals(list.get(i+1))){
       newList.add(list.get(i));
       for(int j=0; j < 4; j++){     **// <== only added 4, not the rest of elements**
          newList.add(0);
       }
   } else {
       newList.add(list.get(i));
   }    
}

结果:newList包含1,0,0,0,0,2

预期结果:newList包含1,0,0,0,0,2,0,0,0,0

请告知,

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

import java.util.*;

public class HelloWorld{

     public static void main(String []args){
        List<Integer> list = new ArrayList<>(
            Arrays.asList(1,2,2,0,0,0,0,0,0,0));
            
        System.out.println("Output1: " + getOutput(list));
        
        list = new ArrayList<>(
            Arrays.asList(0,1,1,3,2,2,0,0,0,0,0,0,0,3,0));
            
        System.out.println("Output2: " + getOutput(list));
     }
     
     public static List<Integer> getOutput(List input){
         
         List<Integer> output = new ArrayList<>();
         
         Collections.sort(input); 
         
         Map<Integer, Integer> elementsCountMap = countFreq(input, input.size());
         int zerosCount =  elementsCountMap.containsKey(0) ? elementsCountMap.get(0) : 0;
         int nonZerosCount = (zerosCount == 0) ? elementsCountMap.size() : elementsCountMap.size() - 1;
         int addingZeroCount = zerosCount/nonZerosCount;
         boolean isOdd = (addingZeroCount > 0) && (zerosCount % addingZeroCount == 0) ? false : true;
         boolean isZeroAdded = false;
         
          for (Map.Entry<Integer, Integer> entry : elementsCountMap.entrySet()) 
        { 
            if(entry.getKey() != 0){
                
                for(int j = 0 ; j < entry.getValue() ; j++)
                    output.add(entry.getKey());
                    
                if(addingZeroCount > 0){
                    for(int j = 0 ; j < addingZeroCount ; j++)
                        output.add(0);
                    if(isOdd && !isZeroAdded)
                        output.add(0);isZeroAdded = true;
                }
            }
              
        } 
        
         return output;
     }
     
     static  Map<Integer, Integer> countFreq(List arr, int n) { 
        Map<Integer, Integer> mp = new HashMap<>(); 
  
        
        for (int i = 0; i < n; i++) 
        { 
            if (mp.containsKey((int)arr.get(i)))  
            { 
                mp.put((int)arr.get(i), mp.get((int)arr.get(i)) + 1); 
            }  
            else
            { 
                mp.put((int)arr.get(i), 1); 
            } 
        } 
        
        return mp;
    }
}