如何计算ArrayList中的重复项并将其作为字符串返回?
例如我有ArrayList [1,3,4,4,5,6,3,4,6]并且我想要有一个方法来计算Array项的出现并在它们出现多个时显示它们输出格式的时间如下:
3 - 2; 4 - 3; 6 - 2;
我尝试过类似的东西
`
import java.util.ArrayList;
import java.util.Random;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
public class GeneratorKostek {
public int ileKostek;
Random rand = new Random();
String wynik;
private ArrayList<Integer> rzuty;
private ArrayList<String> dublety;
private int rzut = 0;
private int ileScian;
private int suma;
public GeneratorKostek() {
rzuty = new ArrayList<Integer>();
}
public GeneratorKostek(int IleKostek, int IleScian) {
ileKostek = IleKostek;
ileScian = IleScian;
rzuty = new ArrayList<Integer>();
}
public void Rzucaj(int IleKostek, int IleScian) {
ileKostek = IleKostek;
ileScian = IleScian;
for (int i = 0; i < ileKostek; i++) {
rzut = rand.nextInt(ileScian) + 1;
rzuty.add(rzut);
}
}
public String PrezentujRzuty() {
wynik = "";
for (int i : rzuty) {
wynik = wynik + i;
}
return wynik;
}
public void CzyscRzuty() {
rzuty.clear();
}
public int SumaWartosciRzutow() {
suma = 0;
for (int i : rzuty) {
suma += i;
}
return suma;
}
public String PrezentacjaDubletow() {
Set<Integer> noDupes = new HashSet<Integer>();
noDupes.addAll(rzuty); // Remove duplicates
for (Integer i : noDupes) {
int wystapienia = Collections.frequency(rzuty, i);
if (wystapienia > 1) {
dublety.add(i + " - " + wystapienia + ";" );
}
}
for (int i =0 ; i < dublety.size(); i++) {
wynik = wynik + dublety.get(i);
}
return wynik;
}
}
`
我刚刚开始编程的冒险......
我在其他课程中称呼它
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
GeneratorKostek Kostki = new GeneratorKostek();
ArrayList<Integer> wyniki = new ArrayList<Integer>();
String wynik;
Button btnk6, btn2k6, btn3k6, btn4k6, btn5k6, btn6k6;
TextView tvWynik;
EditText etInfo;
Random rand = new Random();
int kostka1, kostka2, kostka3, kostka4, kostka5, kostka6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnk6 = (Button) findViewById(R.id.btnk6);
btn2k6 = (Button) findViewById(R.id.btn2k6);
btn3k6 = (Button) findViewById(R.id.btn3k6);
btn4k6 = (Button) findViewById(R.id.btn4k6);
btn5k6 = (Button) findViewById(R.id.btn5k6);
btn6k6 = (Button) findViewById(R.id.btn6k6);
tvWynik = (TextView) findViewById(R.id.tvWynik);
etInfo = (EditText) findViewById(R.id.etInfo);
btnk6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Kostki.CzyscRzuty();
Kostki.Rzucaj(1, 6);
tvWynik.setText(Kostki.PrezentujRzuty());
}
});
btn2k6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Kostki.CzyscRzuty();
Kostki.Rzucaj(2, 6);
tvWynik.setText(Kostki.PrezentujRzuty());
}
});
btn3k6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Kostki.CzyscRzuty();
Kostki.Rzucaj(3, 6);
tvWynik.setText(Kostki.PrezentujRzuty());
}
});
btn4k6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Kostki.CzyscRzuty();
Kostki.Rzucaj(4, 6);
tvWynik.setText(Kostki.PrezentujRzuty());
}
});
btn5k6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Kostki.CzyscRzuty();
Kostki.Rzucaj(5, 6);
tvWynik.setText(Kostki.PrezentujRzuty());
}
});
btn6k6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Kostki.CzyscRzuty();
Kostki.Rzucaj(6, 6);
tvWynik.setText(Kostki.PrezentujRzuty());
etInfo.setText(Kostki.PrezentacjaDubletow());
}
});
// if (savedInstanceState == null) {
// getSupportFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment())
// .commit();
// }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}`
答案 0 :(得分:3)
对列表进行排序,然后很容易计算项目。
答案 1 :(得分:0)
使用Map
中每个唯一项目的keys
创建一个ArrayList
对象。
遍历您的List
,查询Map
以查看该项目是否有条目。如果有,则增加存储的值,否则用1初始化。
编辑*:这是一个例子
package com.count.list.items;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListItemCounter
{
public void start()
{
List<Integer> list = getList(1, 1, 5, 3, 7, 3, 11, 2, 3, 1);
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer i : list)
{
Integer retrievedValue = map.get(i);
if (null == retrievedValue)
{
map.put(i, 1);
}
else
{
map.put(i, retrievedValue + 1);
}
}
System.out.println("list: " + list);
printCount(map);
}
private List<Integer> getList(int... numbers)
{
List<Integer> list = new ArrayList<Integer>();
for (int i : numbers)
{
list.add(i);
}
return list;
}
private void printCount(Map<Integer, Integer> map)
{
for (Integer key : map.keySet())
{
System.out.println("number: " + key + ", count: " + map.get(key));
}
}
}
答案 2 :(得分:0)
试试这段代码: public static void main(String [] args){
String input = "I am am akash pal pal pal";
System.out.println("Before removing duplicate: "+input);
String[] arr = input.split(" ");
int count=0;
System.out.println(arr.length);
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i].equalsIgnoreCase(arr[j])) {
//System.out.println("Duplicates are \n"+arr[i]);
arr[j] = null;count++;
}
}
}
}
System.out.println("After");
for (int k = 0; k < arr.length; k++) {
if (arr[k] != null)
System.out.print(arr[k] + " ");
}
System.out.println("");
System.out.println("Duplicates \t"+count);