我有一个对象的ArrayList.每个对象都是Player类型(我拥有的类之一).每个Player对象都有一个getName()方法和一个getValue()方法.getValue方法是整数类型.所有Player对象都进入ArrayList,listOfPlayers.如何找到getValue()最高的PLayer对象?
我知道有一个名为Collections.max()的方法,但对我来说,似乎只有ArrayList只是整数才有效.
谢谢
我有以下课程
public class Patient implements Serializable {
private int id;
private String name;
private String desc;
public Patient(int id, String name, String desc) {
this.id = id;
this.name = name;
this.desc=desc;
}
Run Code Online (Sandbox Code Playgroud)
然后是这个类中的以下getter和setter方法.
宣布arraylist
ArrayList<Patient> list = new ArrayList<Patient>();
Run Code Online (Sandbox Code Playgroud)
在列表中添加运行时数据
list.add(new Patient(id++, name.getText(), disDesc.getText())
Run Code Online (Sandbox Code Playgroud)
现在我想读取存储在列表中的元素我该怎么做?
System.out.println(list.get(0));
Run Code Online (Sandbox Code Playgroud)
我使用它但它返回类的对象地址
我有一个ArrayList有序的Integer数字.我需要在列表中插入一个元素,但我需要知道插入应该在什么位置进行以保持排序.
如果我只是添加它并使用,Collections.sort()那么我将无法找到插入元素的位置.
我有一个ArrayList变量的问题,它包含Videos我创建的类的对象.在ArrayList<Videos>我创建似乎永远保持为空,即使我add给它的对象.这是我的代码,我声明并使用此ArrayList:
public class Search extends Activity implements View.OnClickListener {
SearchView buttonSearch;
public static String URL_ALLVIDEOS = "http://" + Connexion.IP_ADRESS + "/protubes_android/getVideos.php";
public String[] videosArray;
public ArrayList<Videos> videosListe = new ArrayList<Videos>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_search);
RetrieveVideos rv = new RetrieveVideos();
rv.execute();
work();
}
private void initialization() {
buttonSearch = (SearchView) findViewById(R.id.svSearch);
buttonSearch.setOnSearchClickListener(this);
}
public void work(){
videosArray = new String[videosListe.size()];
for (int i = 0; i < videosListe.size(); i++) {
videosArray[i] …Run Code Online (Sandbox Code Playgroud) 我想从列表开始构建一个数组数组,但是我得到了一个转换异常.谁知道为什么?这是代码
List<String[]> listofarray=new ArrayList<String[]>();
//...filling the list...
String[][] ob= (String[][]) listofarray.toArray();
Run Code Online (Sandbox Code Playgroud) 我正在尝试从ArrayList中删除类似的字符串,但我收到此错误:
CurrentModificationException
Run Code Online (Sandbox Code Playgroud)
这是我的方法,我传递我的原始arrayList(旧)并获得一个没有冗余字符串的新列表.
ArrayList<String> removeRed(ArrayList<String> old) throws IOException
{
ArrayList<String> newList = new ArrayList<String>();
for (int i=0; i< old.size(); i++)
{
if(newList.size() < 1)
{
newList.add(old.get(0));
} else{
for(Iterator<String> iterator = newList.iterator(); iterator.hasNext();) {
while(iterator.hasNext())
{
if(!ChopMD((String) iterator.next()).equals(ChopMD(old.get(i))))
{
newList.add(old.get(i));
Log.e("new algo", "" + old.get(i) );
}
}
}
}
}}
Run Code Online (Sandbox Code Playgroud)
请注意,我的ChopMD()返回一个特定的字符串,它工作正常.它适用于前几个字符串,它抛出异常.任何解决此问题的建议都将受到赞赏.谢谢.
ArrayList调整当前底层数组的大小或创建一个新数组?我读过有时会引用ArrayList的"可调整大小的数组",但我认为每次"调整大小"时它都会被创建一个新的.实际上会发生什么,相同的数组会被调整大小或每次都创建一个新数组(容量限制结束)?
谢谢,确实是的
尝试运行此代码时,我收到"找不到符号"错误.这是关于List代码行的.它将它扔在List和ArrayList上.我无法想象这是如何实现错误的.该脚本调用一个名为Employee的类.List将包含从Employee创建的所有对象.然后应该能够打印List.
import java.util.Scanner;
class PayrollProgram
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
List<Employee> employees = new ArrayList<>();
while (!emp.name.equals("STOP"))
{
Employee emp = new Employee();
System.out.print("Employee's Name: ");
emp.name = scan.next();
if(emp.name.equals("STOP"))
{
System.out.printf("The Application is STOPPING......");
break;
}
System.out.print("Enter hourly wage: $ ");
emp.wage = scan.nextDouble();
while (emp.wage < 0)
{
System.out.printf("Please Enter a Positive Number! \n");
System.out.print("Enter hourly wage: $ ");
emp.wage = scan.nextDouble();
}
System.out.print("Hours Worked in Week: ");
emp.hours = scan.nextDouble(); …Run Code Online (Sandbox Code Playgroud) 为什么我在执行以下代码时遇到异常.
String[] array1 = {"A","B","C","D","E"};
String[] array2 = {"F","G","H","I"};
List<String> list = Arrays.asList(array1);
list.addAll(Arrays.asList(array2)); //here getting exception
Run Code Online (Sandbox Code Playgroud)
例外
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractCollection.addAll(Unknown Source)
at Sample6.main(Sample6.java:19)
Run Code Online (Sandbox Code Playgroud)
但是这段代码运作良好.
List<String> list = new ArrayList<String>(Arrays.as.asList(array1));
list.addAll(Arrays.asList(array2));
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试在java上写一个solitiare游戏.我正在尝试将卡对象添加到我的arraylist但MakeDeck功能无法正常工作.
public class PileOfCards {
public ArrayList<Card> pile = new ArrayList<Card>();
Card tempo;
public PileOfCards() {
pile = new ArrayList<Card>();
}
public void MakeDeck() {
for (int i=0;i<13;i++){
tempo = new Card(i+1, "FaceDown", "Clubs");
pile.add(tempo);
}
for (int j=0;j<13;j++){
tempo = new Card(j+1, "FaceDown", "Diamonds");
pile.add(tempo);
}
for (int k=0;k<13;k++){
tempo = new Card(k+1, "FaceDown", "Hearts");
pile.add(tempo);
}
for (int l=0;l<13;l++){
tempo = new Card(l+1, "FaceDown", "Spades");
pile.add(tempo);
}
}
Run Code Online (Sandbox Code Playgroud)
当我试图打印值时,它打印"0,null,null",52次.问题是什么?看起来我无法访问ArrayList,但我不知道为什么.
编辑:
卡构造函数:
public Card(int valueTemp, String suitTemp, String statusTemp) …Run Code Online (Sandbox Code Playgroud)