标签: arraylist

仅获取与条件匹配的ArrayList的第一个元素

我一直试图找到可能的答案,但没有发现.

我有ArrayList一整套自定义对象.他们的一个领域是boolean.

我想把这个对象放在第一位,保留其余的元素

例如,如果我有这个列表并且obj5是将此布尔值设置为true的那个:

obj3, obj2, obj5, obj7, obj9
Run Code Online (Sandbox Code Playgroud)

我想得到这个:

obj5, obj3, obj2, obj7, obj9
Run Code Online (Sandbox Code Playgroud)

编辑:不能使用LAMBDAS,JAVA 6

编辑2:请注意,列表的其余部分必须遵守旧订单

编辑3:简而言之,我需要这个程序输出[B,A,C,D,E]:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Trip {

@Override
public String toString() {
    return name;
}

private String name;
private boolean freeCancellation;


public Trip(String name, boolean freeCancellation) {
    this.name = name;
    this.freeCancellation = freeCancellation;
}

static Comparator<Trip> myOrder = new Comparator<Trip>() {
    public int compare(Trip a, Trip b) { …
Run Code Online (Sandbox Code Playgroud)

java collections arraylist

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

按每个字符串中的逗号后的值按字母顺序对ArrayList进行排序

我有一个包含字符串的ArrayList,如下所示:

  • 第1项:一些文字,Annabelle
  • 第2项:有些文字,詹姆斯
  • 第3项:一些文字,康纳

我想按照每个String中逗号后的值按字母顺序对ArrayList进行排序.

我怎样才能做到这一点?

    ArrayList<String> lineList = new ArrayList<>();

    // New BufferedReader.
    BufferedReader reader = new BufferedReader(new FileReader(
            file));

    // Add all lines from file to ArrayList.
    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        lineList.add(line);
    }

    // Close it.
    reader.close();

    // Print each line.
    for (String line : lineList) {
        System.out.println(line);
    }
Run Code Online (Sandbox Code Playgroud)

java arrays string arraylist

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

使用rxjava2遍历arraylist

我有一个包含一组对象的arraylist。我想使用rxjava,这样我就可以通过onSubscribe方法循环遍历列表,而不是一次获取整个列表,而是一次获取每个列表项

java android arraylist rx-java rx-java2

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

项目不会添加到我的ArrayList或它们不显示

我是编程新手,我可能会问一个愚蠢的问题,但我真的需要帮助.

我正在学习集合如何工作,我开始ArrayList.

我决定制作ArrayList扑克牌,添加我想在实践中测试的方法(显示元素,显示特定元素等).它不起作用.当我将类型从更改Arraylist为常规数组(Karta[] karty)时,它的工作方式应该如此,所以我知道我正在对集合做错.问题是我不知道是什么因为我刚开始使用colections.

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main {
    static ArrayList<Karta> karty = new ArrayList<Karta>();
    static Random rand = new Random();
    static int wartosc;
    static int kolor;

    public static void utworz(){
        while(wartosc !=0){
            wartosc = rand.nextInt(13);
            kolor = rand.nextInt(3);
            Karta karta = new Karta(wartosc, kolor);
            karty.add(karty.size()-1,karta);
        }
    }

    public static void wyswietl(){
        for(int i=0; i<karty.size();i++)
            System.out.println(karty.toString());
        }

    public static int wyswietlIlosc(){
      return karty.size();
    }

    public static void …
Run Code Online (Sandbox Code Playgroud)

java collections arraylist

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

我不明白为什么这个程序不起作用.多态性

我最近开始编程,我忙于继承,多态和覆盖.现在我刚刚制作了这个程序,由于某些原因它不起作用.编译器强调"= new Arraylist();但我不知道为什么.

代码如下:

超级:人

public class Person {
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

public void print() {
    System.out.println("\t" + firstName + " " + lastName);
}
}
Run Code Online (Sandbox Code Playgroud)

子类:老师

public class Teacher extends Person {
public Teacher(String firstName, String lastName) {
    super(firstName, lastName);
}

public void print() {
    System.out.println("Professor Details: ");
    super.print();
}

}
Run Code Online (Sandbox Code Playgroud)

子类学生:

public class Student extends Person {
private int graduationYear;
private double …
Run Code Online (Sandbox Code Playgroud)

java polymorphism inheritance overriding arraylist

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

Java:字符串到整数数组

我有一个字符串,它是一个坐标列表,如下所示:

st = "((1,2),(2,3),(3,4),(4,5),(2,3))"
Run Code Online (Sandbox Code Playgroud)

我希望将其转换为坐标数组,

a[0] = 1,2
a[1] = 2,3
a[2] = 3,4
....
Run Code Online (Sandbox Code Playgroud)

等等.

我可以用Python做,但我想用Java做.那么如何在java中将字符串拆分为数组?

java arrays arraylist

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

Java-从变量中删除“可选”(转换)

对于一项家庭作业,我必须设置一个变量。给我的set方法将其转换为“可选”。但是,我必须将此变量存储在不允许可选变量的ArrayList中。如何转换该变量使其不再是可选变量?

设置方法:

 public void setParentVertex(IVertex<T> parentVertex)
    {
    if(parentVertex == null)
        this.parentVertex = Optional.empty();
    else
        this.parentVertex = Optional.of(parentVertex);
}
Run Code Online (Sandbox Code Playgroud)

我在尝试使用它的地方:

 ArrayList<IVertex<T>> path = new ArrayList<IVertex<T>>();
 IVertex<T> parent = current.getLabel().getParentVertex();
 path.add(parent);
Run Code Online (Sandbox Code Playgroud)

我不断收到的错误是:“我在其中声明变量“ parent”的那一行出现了“错误:不兼容的类型:Optional>无法转换为IVertex”。谢谢。

java arraylist data-conversion optional

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

从哈希图/数组列表中打印百里香中的键/值

java / springboot中的代码:

@RequestMapping(value = "results")
public String results(
        Model model, 
        @RequestParam String searchType,
        @RequestParam String searchTerm) {

    model.addAttribute("columns", ListController.columnChoices);
    ArrayList<HashMap<String, String>> jobsbyval = JobData
            .findforValue(searchTerm);
    model.addAttribute("items", jobsbyval);
    return "search";
}
Run Code Online (Sandbox Code Playgroud)

html / thymeleaf中的代码:

<div>
  <table>
    <tbody>
      <tr th:each="item : ${items}">
        <!--each loop begins -->
        <td th:text="${item}"></td> //item.value or item.key dont work!!

      </tr>
      <!--loop ends -->
    </tbody>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)

这是html输出。

{header 1=something, header 2=Analyst, category 3=somename, location=somewhere, skill=Stats}
Run Code Online (Sandbox Code Playgroud)

表格式的所需HTML输出(键/值)为:

header 1  something  
header 2  Analyst 
category  somename 
location  somewhere 
skill …
Run Code Online (Sandbox Code Playgroud)

arraylist hashmap thymeleaf

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

java中的Push_back和pop_back

我来自C++背景.我使用向量push_back和pop_back方法来推送和弹出向量中的元素.我知道arraylist等同于vector,但我没有在arraylist API中找到与push_back和pop_back等效的方法.我能找到的壁橱是LinkedList.我错过了什么或者在C++中还有其他等价的向量吗?

任何帮助表示赞赏.

c++ java stl vector arraylist

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

Java版本中的ArrayList修改为什么?

ArrayList声明从java版本1 修改为另一个ArrayList.修改声明的优点是什么.

例如:这里我提到了各种版本的Java支持的三种声明.我在Eclipse中设置了Java Compilance leve 1.4

 ArrayList val=new ArrayList();//Supported at 1.4  
 ArrayList<String> val=new ArrayList<String>();//Syntax error, parameterized types are only available if source level is 1.5 or greater 
 ArrayList<String> val=new ArrayList< >();//operator is not allowed for source level below 1.7
Run Code Online (Sandbox Code Playgroud)

java arraylist

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