List in java example w3schools

Adelaide 51 Published: 12/13/2024

List in java example w3schools

I apologize for the confusion earlier! Since you've asked me to respond in English and list some Java examples from W3Schools, I'll do my best to provide you with that. Here are a few Java examples taken directly from W3Schools:

Example 1: Hello World Program

// A simple "Hello World" program

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!"); // prints to the console

}

}

This is a basic Java program that prints out the string "Hello, World!" to the console. This is often the first Java program beginners write.

Example 2: Calculating an Average

// Calculate the average of two numbers

public class Average {

public static void main(String[] args) {

int num1 = 10;

int num2 = 20;

double average = (num1 + num2) / 2.0; // calculate the average

System.out.println("The average is " + average); // prints to the console

}

}

This program calculates the average of two numbers, num1 and num2, using Java's built-in arithmetic operations and System.out.println() function to print out the result.

Example 3: Converting Celsius to Fahrenheit

// Convert Celsius temperature to Fahrenheit

public class Temperature {

public static void main(String[] args) {

double celsius = 30.0; // set the temperature in Celsius

double fahrenheit = (celsius * 9 / 5) + 32; // convert Celsius to Fahrenheit

System.out.println(celsius + "°C is equal to " + fahrenheit + "°F"); // prints to the console

}

}

This program takes a temperature in Celsius as an input and converts it to Fahrenheit using Java's arithmetic operations. It then prints out the converted result.

Example 4: Random Number Generation

// Generate a random number between 1 and 100

import java.util.Random;

public class RandomNumber {

public static void main(String[] args) {

Random rand = new Random();

int randomNumber = rand.nextInt(100) + 1; // generate a random number

System.out.println("The random number is " + randomNumber); // prints to the console

}

}

This program uses Java's Random class to generate a random number between 1 and 100. It then prints out the generated result.

These are just a few examples of what you can do with Java! W3Schools has many more examples and tutorials on their website if you'd like to learn more.

Please let me know if you have any questions or need further clarification on these examples!

list<string java example

Here is a Java example of using the list method with strings:

import java.util.ArrayList;

import java.util.List;

public class Main {

public static void main(String[] args) {

// Create an empty list

List fruitList = new ArrayList<>();

// Add elements to the list

fruitList.add("Apple");

fruitList.add("Banana");

fruitList.add("Cherry");

fruitList.add("Date");

// Print the list

System.out.println("Initial list: " + fruitList);

// Use the list method to retrieve a specific element from the list

String specificFruit = fruitList.get(1);

System.out.println("Specific fruit at index 1: " + specificFruit);

// Iterate over the list and print each element

for (String fruit : fruitList) {

System.out.println(fruit);

}

}

}

When you run this program, it will create an empty list called fruitList, add four string elements to the list, print the initial list, retrieve a specific element from the list using the get method, and then iterate over the list using a for-each loop to print each element.

The output of this program would be:

Initial list: [Apple, Banana, Cherry, Date]

Specific fruit at index 1: Banana

Apple

Banana

Cherry

Date

As you can see, the list method provides an easy way to work with collections of strings in Java.