Java Programming Tutorial 46 - Quickly Initialize a List with Elements & How to Print List

preview_player
Показать описание


~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

🅑 Bitcoin - 3HnF1SWTzo1dCU7RwFLhgk7SYiVfV37Pbq
🅔 Eth - 0x350139af84b60d075a3a0379716040b63f6D3853
Рекомендации по теме
Комментарии
Автор

you can also just System.out.print(grades); since it is a list not an array, or you could

Moooe-xrgj
Автор

Hey Caleb, if you see this. When I used the following code:
List<Integer> grades = Arrays.asList(1, 3, 2, 8);
System.out.println(grades);
It printed out the list values as [1 3 2 8] just fine. How come you needed that line:
?

mauriceallenliddy
Автор

Why was line 9 needed in this video? The integer array?

marco
Автор

Writing grades.toString() works for the print method. We do not need to convert it back to an Array. Correct me if I'm wrong on this.

thilansenanayake
Автор

I tried to just print it out like this and it worked for me the same as the other confusing method:
System.out.println(test); // test is my list

ya-...
Автор

Earned yourself a subscriber. Great job explaining collection!!

deboking
Автор

I tried these methods and they all worked :
System.out.println(grades);


Is there any difference ?

fahamalaamri
Автор

hey Caleb, is it okay if i use
sout(grade.toString());

its working, but i just want to know if its okay?

SkyeVintorez
Автор

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

public class Exercise3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int size;
System.out.print("Enter the maximum number of elements for the List : ");
size = scan.nextInt();
List<Integer> grades = new ArrayList<Integer>();
System.out.println("Enter the elements for the List : ");

for(int i = 0; i < size; i++)
{
grades.add(i, scan.nextInt());
}

System.out.print("Enter an arithmetic operator : ");
char a = scan.next().charAt(0);

switch(a)
{
case '+':
for(int i = 0; i < size; i++)
{


, grades.get(i) + grades.get(i+1));
e){}
}

break;
case '-':
for(int i = 0; i < size; i++)
{


, grades.get(i) - grades.get(i+1));
e){}
}

break;
case '*':
for(int i = 0; i < size; i++)
{


, grades.get(i) * grades.get(i+1));
e){}
}

break;
case '/':
for(int i = 0; i < size; i++)
{


, grades.get(i) / grades.get(i+1));

e){}
}

break;
case '%':
for(int i = 0; i < size; i++)
{


, grades.get(i) % grades.get(i+1));
e){}
}

break;
default:
break;
}
}
}

joeloswin.j