标签: arraylist

将ArrayList <customtype>转换为CharSequence []

我有一个自定义ArrayList,并希望将其转换为CharSequence [].

我试过这个

List<String> list = Arrays.asList("foo", "bar", "waa");
CharSequence[] cs = list.toArray(new CharSequence[list.size()]);
System.out.println(Arrays.toString(cs)); // [foo, bar, waa]
Run Code Online (Sandbox Code Playgroud)

但它只适用于字符串类型.

java android arraylist charsequence

0
推荐指数
1
解决办法
4384
查看次数

使用Java中的数组列表遇到超出范围的异常错误

我正在尝试为Java中的arraylists创建一个bubble swap方法,但是我遇到了一个错误.这是我的代码:

public static void BubbleSort()
{
    list.remove("");
    for (int i = list.size(); i > 0; i--)
    {
        for (int j=0; j < i; j++)
        {
            if(Integer.valueOf((String) list.get(j)) < Integer.valueOf((String) list.get(j + 1)))
                Swap(list.get(j), list.get(j + 1));
        }
    }
    System.out.println(list);
}
Run Code Online (Sandbox Code Playgroud)

这是它调用的Swap方法:

public static void Swap(Object object, Object object2)
{
    Object spotC = object; 
    list.set(list.indexOf(object), object2); 
    list.set(list.indexOf(object2), spotC);
}
Run Code Online (Sandbox Code Playgroud)

java sorting arraylist bubble-sort indexoutofboundsexception

0
推荐指数
1
解决办法
695
查看次数

在java中使用foreach循环时出现奇怪的错误

我正在做一个小小的todolist程序,我得到了一个我从未有过的奇怪的错误.我有4个类:1个包含todo-data的POJO类:

public class Todo implements Comparable {
  private String title;
  private String task;
  private boolean done;

  public Todo(String title, String task) {
    this.title = title;
    this.task = task;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String newTitle) {
    title = newTitle;
  }

  public String getTask() {
    return task;
  }

  public void setTask(String newTask) {
    task = newTask;
  }

  public boolean isDone() {
    return done;
  }

  public void setDone(boolean isDone) {
    done = isDone;
  }

  public int compareTo(Object …
Run Code Online (Sandbox Code Playgroud)

java foreach arraylist

0
推荐指数
1
解决办法
1078
查看次数

只有最后一个Object被添加到ArrayList中

我创建了一个用户定义的数据类型并从文件中读取数据.以下是代码:

学生班:

package system.data;

public class Student {

private String firstName;
private String lastName;
private String regNumber;
private int coursework1Marks;
private int coursework2Marks;
private int finalExamMarks;
private double totalMarks;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getRegNumber() {
    return regNumber;
}

public void setRegNumber(String regNumber) {
    this.regNumber = regNumber;
}

public int getCoursework1Marks() …
Run Code Online (Sandbox Code Playgroud)

java arrays arraylist object

0
推荐指数
1
解决办法
1329
查看次数

排除PriorityQueue(Java)算法的麻烦:为什么排序不同于排序的ArrayList?

我正在研究霍夫曼.但我发现PriorityQueue的排序算法存在问题; 它没有足够的比较!然后我写了一个简单的类来测试Collections的排序和PriorityQueue的排序:

public class Student implements Comparable<Student>{

    String name;
    int score;
    int math;

    public Student(int score, int math, String name) {
       this.name = name;
       this.score = score;
       this.math = math;
    }

    public int compareTo(Student other) {
       if (this.score > other.score) {
           return -1;
       } else if (this.score < other.score) {
           return 1;
       } else {
           if (this.math > other.math) {
               return -1;
           } else {
               return 1;
           }
       }

       return 0;
   }

   public String toString() {
       return("[" + name + …
Run Code Online (Sandbox Code Playgroud)

java sorting algorithm arraylist priority-queue

0
推荐指数
1
解决办法
599
查看次数

将linkedList转换为数组列表时出现ArrayIndexOutOfBoundsException

我试图将linkedList转换为ArrayList,如下所示.

private LinkedList<myData> myLinkedList= new LinkedList<myData>();  
public Collection<myData> getData()  
 {
  return new ArrayList<myData>(myLinkedList);  
 }
Run Code Online (Sandbox Code Playgroud)

LinkedList可能由多个线程更新.在生产中测试时,我得到以下错误.错误不一致.所以我得到它可能是一个星期,一个月左右.

java.lang.ArrayIndexOutOfBoundsException: 15   
at java.util.LinkedList.toArray(LinkedList.java:866)   
at java.util.ArrayList.<init>(ArrayList.java:131)   
at org.xxx.yyy.zzz.getData(Data.java:291)  
Run Code Online (Sandbox Code Playgroud)

有没有什么办法可以与LinkedList的并发修改有关.感谢任何帮助.

java linked-list arraylist

0
推荐指数
1
解决办法
1175
查看次数

在Java中,如何比较两个对象的值?

我想构建和IP地址管理工具.我想在数据库中存储使用过的IP,其中包含使用IP的设备的信息.我希望能够将"已使用"的ips拉出来并将它们与来自给定子网的IP列表进行比较,并确定ip是"已使用"还是"可用".我需要"可用"列表来生成一个列表,我可以选择添加到新设备,而不选择"已使用"的IP.

这是我尝试过的,但它似乎并没有比较IPAddress类型的两个对象.

    ArrayList<IPAddress> used = new ArrayList<>();
    ArrayList<IPAddress> available = new ArrayList<>();
    ArrayList<IPAddress> subnet = new ArrayList<>();

    //I reference my IPAddress class which has four int fields:
    //oct_1, oct_2, oct_3, oct_4  
    //the constructor for IPAddress takes for Ints and I build my IPAddress
    //from the for octets.

    //build a list of all ips in the subnet 10.50.2.0 - 10.50.2.255
    //assuming a class C subnet 255.255.255.0
    for(int i = 0; i < 256; i++){
        subnet.add(new IPAddress(10,50,2,i));
    }

    //identify used ips.  Eventually …
Run Code Online (Sandbox Code Playgroud)

java compare arraylist

0
推荐指数
1
解决办法
1095
查看次数

使用toString方法和每个循环打印对象时要返回什么?

我目前有一个toString方法,类似于下面的方法.请忽略对象仅被临时命名.我这样做是为了使每个变量的类型之间没有混淆等:

@Override
public String toString() {
    for(Object object : ArrayList) {
        System.out.println("This object is a " + object.getVariableA() + " and a " + object.getVariableB() + ".");
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

但是toString方法要求我返回一个值.我显然只想返回我正在打印的字符串,虽然如果我在那里放置一个返回语句,它只会打印一个Object而不是所有我正在循环的对象.什么是打印所有这些值的最佳方法,而不是简单地返回null,因为我不希望在所有对象之后打印出来?我还想确保这些对象中的每一个都按照它们当前的单独行打印,所以请不要建议包含一个长连接字符串而不包含换行符的解决方案,因为这不适合这种情况.

提前致谢!

java foreach arraylist tostring object-to-string

0
推荐指数
1
解决办法
2941
查看次数

显示arrayList <int []>的内容

下面的程序生成10对特定大小的随机数,并将它们存储在名为test的ArrayList中 -

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class randomGenerate
{
    static ArrayList<String> tcase=new ArrayList<String>();
    static ArrayList<int[]> test=new ArrayList<int[]>();

    public static void main(String args[])
    {

        tcase.add("1");
        tcase.add("2");
        tcase.add("3");
        tcase.add("4");
        tcase.add("5");
        tcase.add("6");
        randomSelection(10,2);
        for(int i=0;i<test.size();i++)
        {
            System.out.println(Arrays.toString(test.get(i)));
        }
    }

    static int randomNo(int max,int min)
    {
        Random obj = new Random();
        int n = max - min + 1;
        int i = obj.nextInt(n);
        int randomNum =  min + i;
        return randomNum;   
    }

    static void randomSelection(int limit, int pairSize) 
    { …
Run Code Online (Sandbox Code Playgroud)

java arraylist

0
推荐指数
1
解决办法
106
查看次数

从Java中删除ArrayList中的对象出错[Eclipse]

我有这个方法从ArrayList 中删除特定的对象P.

这是我的代码:

public void removeProduct(Product p) throws StockException{
        int flag=0;
        for(Product i:StockProducts)
            if(p.getCode()==i.getCode()){
                this.StockProducts.remove(p);
                flag=1;
            }

        if(flag==0){
                StockException e = new StockException("could not remove the Product , PRODUCT IS NOT IN THE STOCK:  ", p);
        throw e;
          }
    }
Run Code Online (Sandbox Code Playgroud)

错误:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at Stock.removeProduct(Stock.java:29)
    at Test.main(Test.java:18)
Run Code Online (Sandbox Code Playgroud)

如果您需要有关我的代码的更多信息,请告诉我

添加方法

public void addProduct(Product p) throws StockException{
        for(Product i:StockProducts)
            if(p.getCode()==i.getCode()){
                        StockException e = new StockException("could not add the Product , CODE ALREADY …
Run Code Online (Sandbox Code Playgroud)

java collections arraylist concurrentmodification

0
推荐指数
1
解决办法
1858
查看次数