这是我的代码:
class mynode
{
mynode prev;
mynode next;
int value;
}
class link
{
List<mynode> pos;
link(int x)
{
pos = new ArrayList<mynode>();
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里看到使用List比使用ArrayList更好.那我为什么看到下面的错误呢?
The type List is not generic; it cannot be parameterized with arguments <mynode>
Run Code Online (Sandbox Code Playgroud) 是否可以ArrayList在扩展的类中使用Thread?我已经写了一些代码来从蓝牙序列中读取它正在工作,但我添加了一个ArrayList跟踪我正在阅读的内容.但是,我正在NullPointException访问任何访问结果的行.我知道这ArrayList不是线程安全的(虽然我可能不完全理解这意味着什么)所以我可能会尝试做不可能的事情,但我想我会与专家核实,因为ArrayLists非常容易使用.
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private List<String> results;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
List<String> results = new ArrayList<String>();
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e); …Run Code Online (Sandbox Code Playgroud) 我试图使用compareToIgnoreCase对字符串数组进行排序.
该字符串包含名称ex:Billy Jean
当我尝试比较它们时,我得到一个空指针异常.我认为这是因为名字和姓氏之间的空格.我如何比较全名?
谢谢
class CompareStudentNames implements Comparator{
//Sorts the students by name ignoring case.
@Override
public int compare(Object o1, Object o2) {
String name1 = ((Student)o1).getName();
String name2 = ((Student)o2).getName();
return (name1.compareToIgnoreCase(name2));
}
Run Code Online (Sandbox Code Playgroud)
}
编辑---在使用比较学生姓名的代码中添加
private static void newInputFileReading(File f) throws FileNotFoundException{
String inputLine = null;
String [] inputSplit = new String[7];
Boolean proceed = false;
Scanner enterReader = new Scanner(System.in);
String name;
while(!proceed){
int stunum = -1;
try {
Scanner inputReader = new Scanner(f);
while(inputReader.hasNextLine()){
studentNM.add(new Student()); …Run Code Online (Sandbox Code Playgroud) 我已经为以下场景做了很多工作,但我无法获得所需的输出.
我被困在我所拥有的场景中:
Arraylist1 = {10,20,30};
Arraylist2 = {4,5,7}
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
Combined: [10, 4, 10, 5, 10, 7, 20, 4, 20 ,5, 20, 7, 30, 4, 30, 5, 30, 7]
Run Code Online (Sandbox Code Playgroud)
我做的所有人都遵循:
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(40);
ArrayList<Integer> numbers2 = new ArrayList<Integer>();
numbers2.add(4); numbers2.add(5); numbers2.add(7);
List<Integer> interleaved = interleave(numbers, numbers2);
System.out.println("\nCombined: " + interleaved);
public static List<Integer> interleave(final List<Integer> list1, final List<Integer> list2) {
List<Integer> result = new ArrayList<Integer>(list2.size() + list1.size());
Iterator<Integer> it1 = list1.iterator();
Iterator<Integer> it2 = list2.iterator(); …Run Code Online (Sandbox Code Playgroud) for(int j = 0; j < arrayList.size(); j++) {
System.out.print(arrayList.get(j));
}
Run Code Online (Sandbox Code Playgroud)
我想以5个为一组进行打印,但代码看起来如何?我是否必须将元素存储到变量中并以这种方式打印?
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
//import java.util.Map.Entry;
public class teste{
public static void main(String[] args){
}
}
Run Code Online (Sandbox Code Playgroud)
我只是希望arraylist每个键的最终用于显示目的,即:
键的返回值:[silicone baby dolls for sale, ego ce4, venus, sample,blue]
键的返回值:[apple, banana, key, kill]
是否可以执行以下操作?
List<String> list = new ArrayList<String>();
Iterator<String> iterator = list.listIterator();
populateList(list);
Run Code Online (Sandbox Code Playgroud)
我在使用数据填充列表之前创建一个列表并获取其迭代器.我能使用迭代器获取列表的元素吗?获取迭代器和填充数组的顺序是否会有所不同?
任何帮助将不胜感激.
for(JCheckBox currentCheckBox : imagesToBeImportedCheckBox){
if(currentCheckBox.isSelected()){
System.out.println("The text Box selected for removing are"+currentCheckBox.getText());
}
}
for(JCheckBox currentCheckBox : imagesToBeImportedCheckBox){
if(currentCheckBox.isSelected()){
System.out.println("I am entering in the loop where this image has to be removed "+currentCheckBox.getText());
imagesToBeImported.remove(currentCheckBox.getText());
}
}
for(ResourceListObject currentImage : imagesToBeImported){
System.out.println("After removing the images left are "+currentImage.getName());
}
Run Code Online (Sandbox Code Playgroud)
这是输出
选择删除的文本框是aix71b
选择要删除的文本框是迁移图像
我进入循环,这个图像必须删除aix71b
我进入循环,必须删除此图像迁移图像
删除后剩下的图像是aix71b
删除图像后,左侧是迁移图像
这是代码本身
import java.util.ArrayList;
public class Student {
private String name;
private int age;
public Student (String n, int a) {
name = n;
age = a;
}
public String toString() {
return name + " is " + age + " years old";
}
ArrayList<Student> rayList = new ArrayList<Student>();
rayList.add(new Student("Sam", 17));
rayList.add(new Student("Sandra", 18));
rayList.add(new Student("Billy", 16));
rayList.add(new Student("Greg", 17));
rayList.add(new Student("Jill", 18));
public static void main(String[] args) {
System.out.println(rayList.get(0));
}
}
Run Code Online (Sandbox Code Playgroud)
main方法中缺少一些println命令.但是当我尝试将5个学生添加到我的ArrayList时,我收到错误"无法对非静态字段rayList进行静态引用"
我为长标题道歉,如果你能想到更好的标题,请告诉我!
我正在做的是尝试创建ArrayLists的ArrayList并逐个添加ArrayLists.AL<AL<I>>我拥有的两个被称为三角形和正方形,我AL<I>s通过addToList()方法添加 - 将被AL<I>调用的temp 添加到适当的AL<AL<I>>.temp似乎没有问题,但是在我运行整个方法figurateNumbers()之后,我AL<AL<I>>s只包含[98,70],这是要添加的最后一个临时值.代码如下:
import java.util.ArrayList;
import java.util.Iterator;
public class problem
{
public static ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>();
public static ArrayList<ArrayList<Integer>> square = new ArrayList<ArrayList<Integer>>();
public static ArrayList<Integer> temp = new ArrayList<Integer>();
public static void figurateNumbers()
//Inserts into individual arraylists, numbers, all figurate numbers square : octagonal
{
for (int ii = 1; ii < 141; ii++)
{
if ((ii * ii >= 1000) & (ii * ii …Run Code Online (Sandbox Code Playgroud) arraylist ×10
java ×10
android ×1
collections ×1
comparator ×1
hashmap ×1
iterator ×1
list ×1
printing ×1
sorting ×1