我有一个String数组:
String [] array={"1","2","3","4","5","6","7","8","9"};
Run Code Online (Sandbox Code Playgroud)
我将其转换为数组列表:
ArrayList<String> al = new ArrayList<String>(Arrays.asList(array));
Run Code Online (Sandbox Code Playgroud)
我想从我的ArrayList中获取一个随机值.
我试图用iterator一会儿迭代列表,由于某种原因,它会无限循环。
这是代码
{
List<Person> Person1 = new ArrayList<Person>();
Person p1 = new Person("Name1", "Virginia");
Person1.add(new Person("Nae2", "Virginia"));
printerlist(Person1);
printerlist(p1);
}
private static void printerlist(List<Person> p) {
/*
* print the list
*/
while (p.iterator().hasNext()) {
System.out.println(p.iterator().next().getCity());
}
}
Run Code Online (Sandbox Code Playgroud) 我已设法将所有对象放入arraylist但我无法打印所有值.无论使用何种方法,都只打印最后一个.
它不是仅通过ArrayList打印,这让我想知道推送的对象是否相同.
如果是,我该如何改变?我已经附加了程序(运行FileInput.java):
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
import javax.swing.text.html.HTMLDocument.Iterator;
//lecture notes(L1) method
public class FileInput {
static String first;
static String second;
static String third;
static String fourth;
static String fifth;
static String sixth;
static int num = 1;
public static void main(String[] args)throws FileNotFoundException, IOException{
Scanner input = new Scanner(new File("player.txt"));
String data = null;
PrintWriter output = new PrintWriter("outfile.txt");
// Player1 user = new Player1();
ArrayList<Player1>listOfPlayers = new …Run Code Online (Sandbox Code Playgroud) 我的ArrayList对象包含3个值.
ArrayList arr=new ArrayList()
arr.Add("abc");
arr.Add("def");
arr.Add("ghi");
Run Code Online (Sandbox Code Playgroud)
我有一个分隔这些值的要求, 分隔符
所以,我用谷歌搜索并得到了解决方案
string afterjoined=string.Join(",", (string[])arr.ToArray(Type.GetType("System.String")));
Run Code Online (Sandbox Code Playgroud)
上面的代码将返回为
ABC,DEF,GHI
但是上面的代码不允许任何字符串与它连接.
我想要一个特殊的字符串,需要与最终的分隔值连接,如下所示,
MYabc,MYdef,Myghi
这是我尝试使用现有代码(哪些不起作用)
string afterjoined=string.Join(",", "MYabc"+(string[])arr.ToArray(Type.GetType("System.String")));
Run Code Online (Sandbox Code Playgroud) 我有一个10个正非零整数的ArrayList.
0: 1103
1: 711
2: 199
3: 1527
4: 1745
5: 1530
6: 984
7: 798
8: 927
9: 1986
Run Code Online (Sandbox Code Playgroud)
当我跑这个
for(int i = 0; i < l1.length; i++){
int temp = l1.retrieveAt(i);
int temp2 = temp % 2;
if(temp2 == 1){
l1.removeAt(i);
}
}
Run Code Online (Sandbox Code Playgroud)
列表更改为
0: 711
1: 1527
2: 1530
3: 984
4: 798
5: 1986
Run Code Online (Sandbox Code Playgroud)
L1是的目的UnorderedArrayList延伸ArrayListClass它实现ArrayListADT(点击引擎收录)
retrieveAt(index)并且removeAt(index)是ArrayListClass的方法.
我已经编写了一个代码来获取arrayList的内容,但是我得到的是随机数作为输出.
Student.java
package com.example.com.arraylist;
public class Student {
int Enrno;
String Name;
public Student(int Enrno, String Name){
this.Enrno=Enrno;
this.Name=Name;
}
}
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
package com.example.com.arraylist;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Student s1=new Student(1,"Rahul");
Student s2=new Student(2,"Rajiv");
Student s3=new Student(3,"Rohan");
ArrayList<Student> studentArrayList=new ArrayList<Student>();
studentArrayList.add(s1);
studentArrayList.add(s2);
studentArrayList.add(s3);
for(Student a:studentArrayList)
System.out.println(a);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
I/System.out: com.example.com.arraylist.Student@d6765f3
com.example.com.arraylist.Student@308a2b0
com.example.com.arraylist.Student@5f1529
Run Code Online (Sandbox Code Playgroud)
我想使用这个for循环本身显示内容.谢谢.
我把这两个插入ArraysList到这个List:
ArrayList<String> category = new ArrayList<>();
ArrayList<Integer> number = new ArrayList<>();
List list = new ArrayList<>();
category.add("cat");
category.add("dog");
category.add("fish");
category.add("Hamster");
number.add(1);
number.add(2);
number.add(3);
number.add(4);
list.add(category);
list.add(number);
Run Code Online (Sandbox Code Playgroud)
现在我如何获得此列表中每个数组的大小?
给出一个对象列表
// ["A", ["B", null, "C"], "D", ["E"], [], null, "F"];
List<Object> objects = Arrays.asList("A", Arrays.asList("B", null, "C"), "D", Arrays.asList("E"), Arrays.asList(), null, "F");
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以将非空值组合到一个列表中["A", "B", "C", "D", "E", "F"]?
有些人坚持认为帖子必须显示尝试的内容
objects.stream().map(o -> {
if (o instanceof List) {
return ((List) o).stream();
} else return objects.stream();
}).filter(Objects::nonNull).collect(Collectors.toList()));
Run Code Online (Sandbox Code Playgroud)
显然上面没有用.
ArrayList<Integer> list = Arrays.asList(new Integer[] {1,2,3,4,5,6,1,8,9});
Run Code Online (Sandbox Code Playgroud)
我想获取(多个)包含元素“ 1” 的第二次发现的索引,但将始终返回(因为这是第一次发现)。list.indexOf(1)0
我想做到这一点而不使用像for或那样的循环while。由于我在游戏中需要它,因此使用循环根本无效。
编辑:有没有办法获得“ indexOf”一些没有迭代器的元素?
基本上我有以下功能需要转换ArrayList为List
public List<List<Integer>> somefunc(int[] nums) {
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
// logic
return a;
}
Run Code Online (Sandbox Code Playgroud)
如何将其转换ArrayList<ArrayList>为List<List>?