[Go to site: main page, start]

0% found this document useful (0 votes)
10 views25 pages

Module 2 Java Arrays

The document provides an overview of arrays in Java, including their declaration, creation, initialization, and traversal methods. It includes examples of various array types, operations such as checking first and last elements, summing elements, and deleting an element from an array. Additionally, it outlines specific problem logic for common array-related tasks.

Uploaded by

divyaclaude
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

Module 2 Java Arrays

The document provides an overview of arrays in Java, including their declaration, creation, initialization, and traversal methods. It includes examples of various array types, operations such as checking first and last elements, summing elements, and deleting an element from an array. Additionally, it outlines specific problem logic for common array-related tasks.

Uploaded by

divyaclaude
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java

JAVA ARRAY
Array in Java
collection of similar data elements stored at contiguous memory locations.
Array stores homogeneous data
Index starts from 0
Size is fixed
In java, Array is an object.
Steps
1. Array Declaration : declare array object
2. Array Creation : size
3. Array Initialization [optional] : values/data/elements
4. Array Traversing : Operations
◦ |- for loop
◦ |- Enhanced for loop
Array Declaration
Declaring an array means creating a reference variable for the array
object.
No memory is allocated at this stage.

int[] arr; // preferred


// OR
int arr[];
Array Creation
Memory is allocated using new Keyword.
Array size must be specified.

Default values stored:


arr = new int[5]; •int → 0
•float → 0.0
•boolean → false
•object → null
class IntArray {
public static void main(String[] args) {
int[] a = new int[5];
[Link](a[0]);
}
}
class FloatArray {
public static void main(String[] args) {
float[] f = new float[5];
[Link](f[2]);
}
}
class BooleanArray {
public static void main(String[] args) {
boolean[] b = new boolean[5];
[Link](b[4]);
}
}
class StringArray {
public static void main(String[]
args) {
String[] s = new String[5];
[Link](s[1]);
}
}
Assigning Value (Compare Default vs Assigned)

class AssignValue {
public static void main(String[] args) {
int[] a = new int[5];
a[0] = 99;

[Link](a[0]); // assigned value


[Link](a[1]); // default value
}
}
public class Main{
public static void main(String args[]){
//declaration and instantiation of an
array
int a[]=new int[5];
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
[Link](a);
[Link](a[0]);
[Link](a[1]);
[Link](a[2]);
[Link](a[3]);
[Link](a[4]);
}
}
[Link] class Main{
2. public static void main(String args[]){
3. //declaration and instantiation of an array
4. int a[]=new int[5];
5. a[0]=10;//initialization
6. a[1]=20;
7. a[2]=70;
8. a[3]=40;
9. a[4]=50;
10. //traversing array
11. for(int i=0;i<[Link];i++){//length is the property of array

12. [Link](a[i]);
13. }
14. }
15.}
Given an array of ints, return true if the array is length 1 or
more, and the first element and the last element are equal.

sameFirstLast([1, 2, 3]) → false


sameFirstLast([1, 2, 3, 1]) → true
sameFirstLast([1, 2, 1]) → true
Problem Logic
Return true if:
[Link] array length is 1 or more, and
[Link] first element == last element
Otherwise, return false.
public class SameFirstLast {

public static boolean sameFirstLast(int[] nums) {


// check array length and compare first & last element
if ([Link] >= 1 && nums[0] == nums[[Link] - 1]) {
return true;
}
return false;
}

// Test cases
public static void main(String[] args) {
[Link](sameFirstLast(new int[]{1, 2, 3})); // false
[Link](sameFirstLast(new int[]{1, 2, 3, 1})); // true
[Link](sameFirstLast(new int[]{1, 2, 1})); // true
}
}
Given an array of ints, return true if 6 appears as either the first or last
element in the array. The array will be length 1 or more.
firstLast6([1, 2, 6]) → true
firstLast6([6, 1, 2, 3]) → true
firstLast6([13, 6, 1, 2, 3]) → false
Problem Logic
Return true if:
•Array length is 1 or more, and
•First element is 6 OR last element is 6
Otherwise, return false.
public class FirstLast6 {

public static boolean firstLast6(int[] nums) {


return (nums[0] == 6 || nums[[Link] - 1] == 6);
}

public static void main(String[] args) {


[Link](firstLast6(new int[]{1, 2, 6})); // true
[Link](firstLast6(new int[]{6, 1, 2, 3})); // true
[Link](firstLast6(new int[]{13, 6, 1, 2, 3}));// false
}
}
Given an array of ints length 3, return the sum of all the elements.
sum3([1, 2, 3]) → 6
sum3([5, 11, 2]) → 18
sum3([7, 0, 0]) → 7

Given an array of ints, return the sum of the first 2 elements in the array. If the
array length is less than 2, just sum up the elements that exist, returning 0 if the
array is length 0.

sum2([1, 2, 3]) → 3
sum2([1, 1]) → 2
sum2([1, 1, 1, 1]) → 2
class Sum3 {
public static void main(String[] args) {

int[] arr = {1, 2, 3}; // array of 3 numbers

int sum = arr[0] + arr[1] + arr[2]; // adding


all elements

[Link]("Sum = " + sum); //


printing result
}
}
class Sum2 {
public static void main(String[] args) {

int[] arr = {1, 2, 3}; // change values to test

int sum = 0;

if ([Link] >= 2) {
sum = arr[0] + arr[1];
}
else if ([Link] == 1) {
sum = arr[0];
}
else {
sum = 0;
}

[Link]("Sum = " + sum);


}
}
Delete Element from Array

Suppose we want to delete element at index 2.

Input:

[10, 20, 30, 40, 50]

After deleting index 2 (value 30):

[10, 20, 40, 50]


class DeleteElement {
public static void main(String[] args) {

int[] arr = {10, 20, 30, 40, 50};


int indexToDelete = 2; // delete element at index 2

int[] newArr = new int[[Link] - 1];

int j = 0;
for (int i = 0; i < [Link]; i++) {
if (i != indexToDelete) {
newArr[j] = arr[i];
j++;
}
}

// Print new array


for (int i = 0; i < [Link]; i++) {
[Link](newArr[i] + " ");
}
}
}

You might also like