2015-04-03 22 views
-4

Im与ArrayLists挣扎并且我的方法public static <T> Pair<T, Integer> mode(T items[])必须返回最常见的字符(或字符串,或整数或双精度)以及它满足的次数在数组中。假设new String[]{"a","b","a"},所以该方法应返回"a", 2。或者,另一个示例new Integer[]{30,10,10,20}应该返回10, 2(数组10中会遇到两次)。请有人指导我走向正轨?考虑到我不允许使用地图,集合......只有ArrayLists!通用类型:如何从通用类型数组中获取最常见的字符

import java.util.ArrayList; 

public class Mode { 

    public static <T> Pair<T, Integer> mode(T items[]) 
    { 
     ArrayList<T> trackItems = new ArrayList<>(); 

     for(T values: items) 
     { 
      trackItems.add(values); 
     } 

     for(int i = 0; i < trackItems.size(); i++) 
     { 
      int count = (Integer)trackItems.get(i); 
     } 

    } 
} 
    public class Pair<X,Y>{ 
     private X first; 
     private Y second; 

     public Pair(X x, Y y){ 
     this.first = x; 
     this.second = y; 
     } 

     public X getFirst(){ 
     return this.first; 
     } 
     public Y getSecond(){ 
     return this.second; 
     } 

     public boolean equals(Object o){ 
     if(!(o instanceof Pair)){ 
      return false; 
     } 
     Pair p = (Pair) o; 
     return 
      this.first.equals(p.first) && 
      this.second.equals(p.second); 
     } 

     public String toString(){ 
     return String.format("(%s,%s)",first,second); 
     } 

    } 
+1

嗯,你面对什么问题? – Tarik 2015-04-03 20:21:00

+0

如何使用泛型找出最常见的字母或整数? @Tarik – Inferno 2015-04-03 20:22:10

+1

如果您使用的是Java 8,ArrayList具有排序方法,这将使您可以轻松地对项目进行分组和计数。 – spudone 2015-04-03 20:24:37

回答

0

你为什么不使用Pair类你的数组列表来模拟图。

使类型X与您匹配的类型的类型相同,并键入Y int。 first代表字符,而Y代表它发生的次数。

  • 使ArrayListPair
  • 的迭代在你的源列表,并在每次迭代,寻找新的ArrayList对谁的first等于当前的元素的元素。
  • 如果找到一个,则向second元素添加1。
  • 如果你没有找到一个,加一,初始化second元素为1
+0

感谢您的回复@SamIam ...为了使ArrayList的Pair,它是ArrayList > = new ArrayList <>()??? – Inferno 2015-04-03 20:28:26

+0

@Inferno你应该可以检查一下,看看你是否能够正常工作 – 2015-04-03 20:29:25

+0

虽然它不能工作 – Inferno 2015-04-03 20:30:59

0

它假定你会使用这个答案,了解如何处理这样的问题,而不是明目张胆地复制答案如果是这样,完成你的任务。

由于Pair类具有其第二个参数仅作为Integer,我会为简单起见如下进行定义:

public class Pair<T> 
{ 
    private T value; 
    private int count; 

    public Pair (T value, int count) 
    { 
    this.value = value; 
    this.count = count; 
    } 

    @Override 
    public String toString() 
    { 
    return String.format ("Pair: [%s, %s]", value, count); 
    } 
} 

现在,Mode类:

import java.util.ArrayList; 
import java.util.Comparator; 

public class Mode 
{ 
    public static <T extends Comparable> Pair<T> mode (ArrayList<T> items) throws Exception 
    { 
    if (items == null) 
    { 
     throw new NullPointerException ("Items Can't be Null"); 
    } 
    else if (items.size() == 0) 
    { 
     throw new Exception ("Items needs to be more than 0"); 
    } 

    items.sort (Comparator.<T>naturalOrder()); 

    int maxCount = 1; 
    int itemCount = 0; 

    T maxItem = items.get (0); 
    T currentItem = items.get (0); 

    for (T item : items) 
    { 
     if (item.equals (currentItem)) 
     { 
     itemCount++; 
     } 
     else if (itemCount > maxCount) 
     { 
     maxCount = itemCount; 
     itemCount = 0; 

     maxItem = currentItem; 
     currentItem = item; 
     } 
    } 

    return new Pair<T> (maxItem, maxCount); 
    } 
} 

在此,我通过使用T extends Comparable行确保TComparable。这样做可以使用排序方法对ArrayList<T>进行排序。

接下来,在mode功能,我先检查ArrayList<T> itemsnull或在它0项目。如果是这样,我会抛出适当的例外。

接下来我整理了items。因此,如果ArrayListInteger s,且项目为10, 20, 10,则排序后将为10, 10, 20

然后我假定具有最大计数的元素是第一个元素。

在此之后,我遍历项目,看看下一个项目是否等于上一个项目。如果是,我增加itemCount。如果不是,我检查maxCount是否小于itemCount。如果是这种情况,则我更新我的maxItemcurrentItemmaxCount,并重置itemCount并再次重复该过程。

一旦循环结束,我将有在maxItem具有最高频率的值,并且maxCount将保持该频率。

下面是一个示例考试来证明它:

import java.util.ArrayList; 
import java.util.Arrays; 

public class Main 
{ 
    public static void main (String[] args) throws Exception 
    { 
    ArrayList<Integer> integers = new ArrayList<> (Arrays.asList (10, 20, 10)); 
    System.out.println (Mode.mode (integers)); 

    ArrayList<String> strings = new ArrayList<> (Arrays.asList ("a", "b", "a", "a")); 
    System.out.println (Mode.mode (strings)); 
    } 
} 

输出是:
Pair: [10, 2]
Pair: [a, 3]

这是预期的,除非我读你的问题是错误的。

+0

到目前为止,你的作业没有达到零点。 – Tarik 2015-04-04 01:39:57

+0

@Tarik:至少我对这个答案有一个有趣的评论。哈哈。当我回答时,我并没有真正期待任何声望点。事实上,我更高兴我没有收到任何票。大声笑。新来者倾向于提出这样的问题,这里的老帽子通过对这些问题投下赞成票而创造了一个进入障碍。诚然,有些人只是为了他们的任务,并没有真正理解这些概念,但事实是他们都不知道如何解决这个问题,否则他们不会首先问这个问题。所以我的答案我试图解释这种方法。 – 2015-04-04 04:28:20

+0

你说的是真的。 SO通常是无情的。平衡帮助和彻底做功课并不明显。也许我们应该逐渐引导人或者指向相关资源来处理这种情况。 – Tarik 2015-04-04 06:16:36

0

这里有一个(1,虽然稍长,线)不使用任何对象,只是方法解决:

public static <T> Pair<T, Integer> mode(T[] items) { 
    return Arrays.stream(items) 
     .collect(Collectors.groupingBy(o -> o, Collectors.counting())) 
     .entrySet().stream() 
     .sorted((a, b) -> b.getValue() - a.getValue()) 
     .findFirst().get() 
     .map(e -> new Pair(e.getKey(), e.getValue()); 
}