ArrayList 인쇄
Address 개체가 포함 된 ArrayList가 있습니다.
이 ArrayList의 값을 어떻게 인쇄합니까? 즉, Array의 내용,이 경우 숫자를 인쇄합니다.
이 코드를 사용하여 배열의 실제 메모리 주소 만 인쇄 할 수 있습니다.
for(int i = 0; i < houseAddress.size(); i++) {
System.out.print(houseAddress.get(i));
}
Arrays.toString(list.toArray())
클래스에 toString()메서드를 추가 address한 다음
System.out.println(Arrays.toString(houseAddress));
toString()메서드에 대한 사용자 지정 구현을 제공하지 않았으므로 해당 개체의 메모리에 주소를 인쇄 할 기본값을 호출합니다.
주소 클래스의 솔루션 은 다음 toString()과 같은 방법을 재정의합니다.
public class Address {
int addressNo ;
....
....
...
protected String toString(){
return Integer.toString(addressNo);
}
이제 전화 할 때
houseAddress.get(i) in the `System.out.print()` method like this
System.out.print( houseAddress.get(i) )toString()의 Address객체가 호출된다
내가 이해하는 바에서 배열의 ArrayList를 인쇄하려고 시도하고 있으며 표시하는 유일한 방법은
System.out.println(Arrays.deepToString(list.toArray()));
그런 숫자 목록이있는 assium
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
목록을 인쇄하면
//method 1
// Conventional way of printing arraylist
for (int number : numbers) {
System.out.print(number);
}
//method 2
// Lambda Expression to print arraylist
numbers.forEach((Integer value) -> System.out.print(value));
//method 3
// Lambda Expression to print arraylist
numbers.forEach(value -> System.out.print(value));
//method 4
// Lambda Expression (method reference) to print arraylist
numbers.forEach(System.out::print);
당신은 그 말을 ArrayList그 무엇으로부터 반환하기 때문에 배열의 주소를 저장하는 toString통화 또는 그 무엇을있는 거 저장 실제로 때문에?
ArrayList배열 이있는 경우 (예 :
int[] arr = {1, 2, 3};
houseAddress.add(arr);
그런 다음 호출해야하는 배열 값을 인쇄하려면 다음을 수행하십시오 Arrays.deepToString.
for (int i = 0; i < houseAddress.size(); i++) {
System.out.println(Arrays.deepToString(houseAddress.get(i)));
}
public void printList(ArrayList<Address> list){
for(Address elem : list){
System.out.println(elem+" ");
}
}
간단히 다음과 같이 줄 수 있습니다.
System.out.println("Address:" +houseAddress);
출력은 다음과 같습니다. [address1, address2, address3]
이는 ArrayList 클래스 또는 해당 수퍼 클래스가 toString () 함수를 재정의하기 때문입니다.
도움이 되었기를 바랍니다.
괄호 안에 houseAddress.get (i)를 넣고 .toString () 함수를 호출합니다. 즉, 아래를 참조하십시오.
for(int i = 0; i < houseAddress.size(); i++) {System.out.print((houseAddress.get(i)).toString());
}
때문에 자바 8 , 당신은 사용할 수 있습니다 대해 forEach () 에서 방법 의 Iterable 인터페이스를 제공합니다.
기본 방법입니다. 인수로서 기능적 인터페이스 Consumer 를 구현하는 클래스의 객체를 취합니다 . 당신은 세 가지 방법으로 로컬 소비자를 구현할 수 있습니다
으로 annonymous 클래스 :
houseAddress.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
람다 식 :
houseAddress.forEach(s -> System.out.println(s));
또는 메서드 참조 를 사용하여 :
houseAddress.forEach(System.out::print);
이 인쇄 방법은 Iterable 인터페이스의 모든 구현에서 작동합니다 .
이들 모두는 요소가 인쇄되는 방법을 정의하는 방법을 제공하는 반면 toString () 은 하나의 형식으로 목록을 인쇄하도록 강제합니다.
주소 개념을 이해했는지 확실하지 않지만 (여기서 houseAddress라고 가정합니다) ArrayList를 인쇄하는 방법을 찾고 있다면 여기에 있습니다.
System.out.println(houseAddress.toString().replaceAll("\\[\\]", ""));
반복자를 사용할 수 있습니다. 여기에서 할 수있는 가장 간단하고 논란이 적은 일입니다. Say houseAddress에는 데이터 유형의 값이 있습니다.String
Iterator<String> iterator = houseAddress.iterator();
while (iterator.hasNext()) {
out.println(iterator.next());
}
참고 : 다른 답변에서 언급 한 것처럼 향상된 for 루프를 사용할 수도 있습니다.
public static void main(String[] args) {
List<Moyen> list = new ArrayList<Moyen>();
Moyen m1 = new Moyen();
m1.setCodification("c1");
m1.setCapacityManager("Avinash");
Moyen m2 = new Moyen();
m2.setCodification("c1");
m2.setCapacityManager("Avinash");
Moyen m3 = new Moyen();
m3.setCodification("c1");
m3.setCapacityManager("Avinash");
list.add(m1);
list.add(m2);
list.add(m3);
System.out.println(Arrays.toString(list.toArray()));
}
이것은 나에게 도움이되었습니다.
System.out.println(Arrays.toString(codeLangArray.toArray()));
@Override public String toString ()을 주석으로 만들면 동일한 결과를 얻을 수 있습니다. 그러나 toString () 메서드를 구현하면 작동합니다.
public class PrintingComplexArrayList {
public static void main(String[] args) {
List houseAddress = new ArrayList();
insertAddress(houseAddress);
printMe1(houseAddress);
printMe2(houseAddress);
}
private static void insertAddress(List address)
{
address.add(new Address(1));
address.add(new Address(2));
address.add(new Address(3));
address.add(new Address(4));
}
private static void printMe1(List address)
{
for (int i=0; i<address.size(); i++)
System.out.println(address.get(i));
}
private static void printMe2(List address)
{
System.out.println(address);
}
}
class Address {private int addr; 공개 주소 (int i) {addr = i; }
@Override public String toString()
{
Integer iAddr = new Integer (addr);
return iAddr.toString();
}
}
향상된 for 루프 또는 다음과 같은 반복기를 사용할 수도 있습니다.
for (String name : houseAddress) {
System.out.println(name);
}
데이터 유형 houseAddress이 무엇이든 변경할 수 있으며 불필요한 변환을 피할 수 있습니다.
Make sure you have a getter in House address class and then use:
for(int i = 0; i < houseAddress.size(); i++) {
System.out.print(houseAddress.get(i)**.getAddress()**);
}
you can use print format if you just want to print the element on the console.
for(int i = 0; i < houseAddress.size(); i++) {
System.out.printf("%s", houseAddress.get(i));
}
Assuming that houseAddress.get(i) is an ArrayList you can add toString() after the ArrayList :
for(int i = 0; i < houseAddress.size(); i++) {
System.out.print(houseAddress.get(i).toString());
}
A general example:
ArrayList<Double> a = new ArrayList();
a.add(2.);
a.add(32.);
System.out.println(a.toString());
// output
// [2.0, 32.0]
This is a simple code of add the value in ArrayList and print the ArrayList Value
public class Samim {
public static void main(String args[]) {
// Declare list
List<String> list = new ArrayList<>();
// Add value in list
list.add("First Value ArrayPosition=0");
list.add("Second Value ArrayPosition=1");
list.add("Third Value ArrayPosition=2");
list.add("Fourth Value ArrayPosition=3");
list.add("Fifth Value ArrayPosition=4");
list.add("Sixth Value ArrayPosition=5");
list.add("Seventh Value ArrayPosition=6");
String[] objects1 = list.toArray(new String[0]);
// Print Position Value
System.err.println(objects1[2]);
// Print All Value
for (String val : objects1) {
System.out.println(val);
}
}
}
참고URL : https://stackoverflow.com/questions/9265719/print-arraylist
'IT TIP' 카테고리의 다른 글
| AlarmManager에서 활성 PendingIntents 목록 가져 오기 (0) | 2020.11.03 |
|---|---|
| DisplayAttribute.Description 특성 값을 어떻게 표시합니까? (0) | 2020.11.03 |
| PHP에서 비트 마스크를 구현하는 방법은 무엇입니까? (0) | 2020.11.03 |
| ActionBar 위쪽 탐색은 onResume 대신 부모 활동을 다시 만듭니다. (0) | 2020.11.03 |
| 더블은 정말 돈에 부적합합니까? (0) | 2020.11.03 |