我有这门课:
public class Contact {
private String firstname;
private String lastname;
private List<Integer> phoneNumber;
private Scanner in;
public Contact(){
phoneNumber = new ArrayList<>();
firstname = lastname = "";
in = new Scanner(System.in);
}
public void setFirstName(){
firstname = in.nextLine();
}
public void setLastName(){
lastname = in.nextLine();
}
public void setPhoneNumber(){
phoneNumber.add(in.nextInt());
}
public String getFirstName(){
return firstname;
}
public String getLastName(){
return lastname;
}
public Integer getPhoneNumber(int position){
return phoneNumber.get(position);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想制作一个有我联系人的电话簿..我想用它
Arraylist<Contact>
Run Code Online (Sandbox Code Playgroud)
因为它没有固定的大小..当我想用Lastname对这个arraylist进行排序时,我该怎么办?
有没有找到ArrayList包含多少列表?
ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
List<Integer> myOtherList = new LinkedList<Integer>();
我有一个Player的ArrayList.该阵列目前在首先登录到最近登录的玩家中进行排序.
但是,我想从播放器位置排序ArrayList,从高到低.该位置存储在Player类中,由其检索
player.getX();
Run Code Online (Sandbox Code Playgroud)
我想我应该有一个循环,每个项目被添加到一个新的ArrayList,但我不能完全弄清楚如何实际实现它.
我怎样才能做到这一点?
环:
for (int i = 0; i < otherPlayers.size(); i++) {
Player currentPlayer = otherPlayers.get(i);
}
Run Code Online (Sandbox Code Playgroud)
编辑:添加循环示例
我试图从用逗号分隔的文件中读取x,y坐标.但是,元素未正确添加到ArrayList.我在哪里错了?
ArrayList<Double> xpointArrayList = new ArrayList<Double>();
ArrayList<Double> ypointArrayList = new ArrayList<Double>();
try {
BufferedReader input = new BufferedReader(new FileReader(args[0]));
String line;
while ((line = input.readLine()) != null) {
line = input.readLine();
String[] splitLine = line.split(",");
double xValue = Double.parseDouble(splitLine[0]);
double yValue = Double.parseDouble(splitLine[1]);
xpointArrayList.add(xValue);
ypointArrayList.add(yValue);
}
input.close();
} catch (IOException e) {
} catch (NullPointerException npe) {
}
double[] xpoints = new double[xpointArrayList.size()];
for (int i = 0; i < xpoints.length; i++) {
xpoints[i] = xpointArrayList.get(i);
}
double[] ypoints = …Run Code Online (Sandbox Code Playgroud) 我正在尝试一些代码,但我无法编译它.有什么错误吗?我已附上以下代码.
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class Node
{
private int nodeId;
private List<Connection> outboundConnections = new ArrayList<>();
public Node(int nodeId)
{
this.nodeId = nodeId;
}
public void addConnection(Connection connection)
{
this.outboundConnections.add(connection);
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,我遇到了这种错误:
Node.java:9: error: cannot find symbol
private List<Connection> outboundConnections = new ArrayList<>();
^
symbol: class Connection
location: class Node
Node.java:16: error: cannot find symbol
public void addConnection(Connection connection)
^
symbol: class Connection
location: class Node
Node.java:9: error: unexpected type
private List<Connection> outboundConnections …Run Code Online (Sandbox Code Playgroud) 我正在逐字节地读取数据.当我确定我有一个完整的消息时,我需要将它作为字符串传递给另一个函数.有些消息可能非常大,但大小经常变化.您觉得哪种实现最有效:
public test class
{
char[] buffer = new char[MAX_SIZE_7200];
int bufferIndex = 0;
void parseData(ArrayList<Byte> msg, length)
{
while (!msg.isEmpty())
{
buffer[bufferIndex++] = (char) msg.remove(0);
if (isfullmessage)
{
parseData(new String(buffer, 0, bufferIndex);
bufferIndex = 0; //restart and continue parsing data
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要么:
public test class
{
List<Character> buffer = new ArrayList<Character>();
int bufferIndex = 0;
void parseData(ArrayList<Byte> msg, length)
{
while (!msg.isEmpty())
{
buffer.add((char) msg.remove(0));
if (isfullmessage)
{
StringBuilder builder = new StringBuilder(buffer.size());
for (Character ch: …Run Code Online (Sandbox Code Playgroud) 我不是JAVA本地程序员.我正在为Web服务API创建一个客户端.API基本上需要一个数组参数.
我正在解析XML文件,创建记录,然后使用此API进行批量INSERT.
问题是这个API一次只能插入200条记录,这意味着我的阵列在拨打电话时只能有200条或更少的记录.
由于我事先不知道有多少条记录,因此我将记录存储在ArrayList中,然后使用.ToArray()将其转换为数组.
APIObject[] invoiceArray = invoiceObjectlist.toArray(new APIObject[invoiceDetailObjectlist.size()]);
Run Code Online (Sandbox Code Playgroud)
现在由于API提出的200限制问题,我需要以200的块创建这些数组,直到插入ArrayList中的所有记录.
现在我想我可以遍历数组列表并维护一个计数器.当counter = 200时,我可以创建一个新数组,并通过维护索引指针将所有元素插入到该点,或者我可以将元素推送到每200条记录的新数组列表中,并将其转换为数组并执行插入.
在JAVA中执行此操作会有什么更优雅/更有效的方法?
谢谢.
我有一个for循环,我用来遍历arraylist,一旦我找到了我需要的对象,我想在这个对象的set方法中设置一个先前获得的对象,将'not found'的标志设置为false然后突破循环.之后,如果未找到标志仍然为真,我想抛出异常,否则只是到了方法的末尾.
我想我要么滥用中断还是for循环.我一直在抛出异常.
注意,'items'是LibraryItems的arraylist.
for(LibraryItem l : items) {
if(l.equals(item)) {
l.setCheckedOut(patronToRetrieve);
itemNotFound = false;
break;
} else {
itemNotFound = true;
}
}
if (itemNotFound = true) {
throw new CheckInOutException("The item " + item.getTitle() + " does not exist in the catalogue. Sorry, " + patronToRetrieve.getName() + ", you will not be able to check this out at this time.");
} else {
}
Run Code Online (Sandbox Code Playgroud) ArrayList<ArrayList<String>> list1 = new ArrayList<ArrayList<String>>();
ArrayList<String> list2 = new ArrayList<String>();
list2.add("foo");
System.out.println(list2); //[foo]
list1.add(list2);
System.out.println(list1); //[[foo]]
list2.set(0, "bar");
System.out.println(list2); //[bar]
System.out.println(list1); //[[bar]]
Run Code Online (Sandbox Code Playgroud)
上面的代码显示了2个列表.当我添加list2(包含foo)list1它们现在都包含foo.但是当我修改list2时bar,list1也会改变.我一直认为add方法只给出了一份list2对list1我做什么list2也不会改变对list1.
我如何才能加入list2,list1但允许list2在将来自由修改,以免list1再次受到影响?
我正在构建一个Android应用程序,我正在使用一个静态arraylist,我在Splash Activity期间填充,以便我可以在整个应用程序中使用它.但是当我退出并重新进入应用程序时,此数组仍保持其先前的内容.发生了什么事?