ArrayLists (Exercise 1)

preview_player
Показать описание
Java Programming: Solved Programming Question on ArrayLists in Java
Topics Discussed:
1) Creating a list of unique elements taken from the user, then sorting and printing these elements using an ArrayList in Java.

Music:
Axol x Alex Skrindo - You [NCS Release]

#JavaByNeso #JavaProgramming #ArraysInJava #ArrayLists
Рекомендации по теме
Комментарии
Автор

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

System.out.println("Please enter 5 Strings:");

for(int i = 0; i < 5; i++) {
String temp = scr.next();
if(!str.contains(temp)) {
str.add(temp);
}
}

Collections.sort(str);

System.out.println(str);

sahil
Автор

I love u, you have been helping me a lot !

jaferalmoalem
Автор

Collections was getting an error for me on Collections.sort(integers); didn't work in BlueJay, it was saying undeclared variable?

jamesmoran
Автор

3:44 I forgot that there is a function for this, so I wrote this code (it does the same thing):

for(i=1; i<=n; i++) {
System.out.print("x" + i + " = ");
x = ip.nextInt();

ok=1;
for(j=0; j<lista.size(); j++)
if(lista.get(j) == x)
= 0;

if(ok == 1)
lista.add(x);
}

tudorgorea
Автор

Sir plzzzz tell when u will complete full course... Plz answer if u have paid complete tutorials then also we are ready to buy... But plz tell when u will upload full java vedioes

mayankverma
Автор

Didn't know I can use integers.contains! Hahaha I had a hard time but managed to solve it anyways.
1. I added ALL the inputs of the user to the ArrayList
2. I sorted the ArrayList
3. I removed every integer that has a duplicate integer in the ArrayList
***
for(int i = 0; i < integers.size(); i++){
for(int j = (i+1); j < integers.size(); j++){
if(integers.get(i) == integers.get(j)){
integers.remove(j);
j--;
}
}
}

Thank you Neso Academy I learned a lot from you guys already. Keep making videos!

boooost
Автор

For Strings:

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

class Test4 {
public static void main(String[] args) throws IndexOutOfBoundsException{
Scanner scanner = new Scanner(System.in);
ArrayList<String> stringsFromUser = new ArrayList<>();
int numberOfStrings = 0;
String temp = null;

System.out.println("How many Strings do you want to enter?");
numberOfStrings = scanner.nextInt();
scanner.nextLine();

for (int n = 1; n <= numberOfStrings; n++){
System.out.println("Enter string number " + n);

}



for (int n = 0; n < stringsFromUser.size() - 1 ; n++){
temp = new
if (temp.equals(new {
stringsFromUser.remove(n + 1);
n--;
}
}
numberOfStrings = 1;
for (String s : stringsFromUser)
+ ")" + s);
}
}

jejewite
Автор

Why does prints out the elements of the array instead of its address like it was in previous videos when we had to use Arrays.toString()?

marcinnalborczyk
Автор

import java.util.*;

class Check {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

ArrayList<Integer> numbers = new ArrayList<>();

System.out.print("Enter 5 Integers: ");

for(int i = 0; i < 5; i++) {
int n = input.nextInt();

if(!numbers.contains(n))
numbers.add(n);
}

Collections.sort(numbers);
System.out.println(numbers);

}
}

suswithcherry
Автор

import java.util.*;
class HelloWorld {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<>();
Scanner s=new Scanner(System.in);
System.out.println("enter five numbers : ");
for(int i=0;i<5;i++){
list.add(s.nextInt());
}
for(int i=0;i<list.size();i++){
int temp=list.get(i);
for(int j=i+1;j<list.size();j++){
if(temp==list.get(j))
list.remove(j);
}
}
Collections.sort(list);
System.out.println(list);
}
}

bhargavsai
Автор

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();
System.out.println("Try programiz.proenter 5intss : ");
Scanner my = new Scanner(System.in);
for(int i=0;i<5;i++){
int temp = my.nextInt();
if (!arr.contains(temp))
arr.add(temp);
}
Collections.sort(arr);
System.out.println(arr);
}
}

-MustakimMusa