0% found this document useful (0 votes)
2 views

code snippets

The document contains five Java code snippets demonstrating different array operations. These include printing an array, iterating through elements, reversing an array, finding the maximum value, and searching for a specific value. Each snippet is accompanied by its output and a brief explanation of its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code snippets

The document contains five Java code snippets demonstrating different array operations. These include printing an array, iterating through elements, reversing an array, finding the maximum value, and searching for a specific value. Each snippet is accompanied by its output and a brief explanation of its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Public class Main {

Public static void main(String[] args) {

Int[] numbers = {1, 2, 3, 4, 5};

System.out.println(Arrays.toString(numbers));

O/P:

[1, 2, 3, 4, 5]

Reason: Arrays.toString(numbers) converts the array to a string


representation for easy printing.

2) Public class Main {

Public static void main(String[] args) {

Int[] numbers = {1, 2, 3, 4, 5};

For (int I = 0; I < numbers.length; i++) {

System.out.println(numbers[i]);

O/P:

Reason:This loop prints each element of the array by iterating through its
indices.
3) Import java.util.Arrays;

Public class Main {

Public static void main(String[] args) {

Int[] numbers = {1, 2, 3, 4, 5};

Int start = 0;

Int end = numbers.length – 1;

While (start < end) {

Int temp = numbers[start];

Numbers[start] = numbers[end];

Numbers[end] = temp;

Start++;

End--;

System.out.println(Arrays.toString(numbers));

O/P:

[5, 4, 3, 2, 1]

Reason: This code swaps the elements from the beginning and end of the
array, moving toward the center, thus reversing the array.

4) Public class Main {

Public static void main(String[] args) {

Int[] numbers = {1, 2, 3, 4, 5};

Int max = numbers[0];

For (int number : numbers) {

If (number > max) {


Max = number;

System.out.println(“Max value: “ + max);

O/P:

Max value: 5

Reason:The loop checks each element and updates the `max` variable if
the current element is greater.

5) Public class Main {

Public static void main(String[] args) {

Int[] numbers = {1, 2, 3, 4, 5};

Int valueToFind = 3;

Boolean found = false;

For (int number : numbers) {

If (number == valueToFind) {

Found = true;

Break;

System.out.println(“Value found: “ + found);

O/P:

Value found: true


Reason: The loop searches for the specified value and sets `found` to
`true` if it is present in the array.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy