Arrays in Java (Exercise 5)

preview_player
Показать описание
Java Programming: Programming Question on Arrays in Java Programming
Topics Discussed:
1. Writing a program that puts the odd elements of an array before the even elements.

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

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

I ran the for loop from 0 to end, give logic (if arr[i]% !=0) and print odes ones, then in another for loop I print even ones using (if arr[i]==0)
I used print instead of println so that all elements placed like an array. It is the simplest method,

code:
import java.util.*;
public class ArrayEx5 {
public static void main(String[] args) {
int[] arr= {-7, -6, -5, -4, 0, 1, 2, 3, 4, 5};
System.out.println("Before sorting the arrays are: ");


sorting the arrays are: ");
sortOddEven(arr);

}
public static void sortOddEven(int[] arr) {
for(int i=0;i<arr.length;i++) {
if(arr[i]%2!=0)
System.out.print(arr[i]+" ");
}
for(int i=0;i<arr.length;i++) {
if(arr[i]%2==0)
System.out.print(arr[i]+" ");
}

}
}

aniruddhahossen
Автор

Mind challenging Quiz. Love it Neso academy 😍.

abdishakur
Автор

It will be much simpler with ArrayList, but here it is:


public static void main(String[] args) {
//given
int[] array = {1, 2, 4, 5, 7, 9, -2, 3};

// assign a variable since the length with be used more than once
int arrayLength = array.length;

//initialise an array that store results with length equal to initial array
int[] store = new int[arrayLength];

//initialise a temporary variable to store elements in the array
int temp = 0;

// set counters for the even and odd numbers
int oddCounter = 0;
int evenCounter = arrayLength - 1;

// for loop to loop through the array and assign it to the 'store' array

for (int n = 0; n < arrayLength; n++){
temp = array[n];

if(temp % 2 == 0) // use condition (temp % 2 == 0) and NOT (temp % 2 == 1 || temp % 2 == -1) for efficiency
store[evenCounter--] = temp; //pass post decrement so that current value of evenCounter will be passed to store
else
store[oddCounter++] = temp; //same logic with evenCounter, only this time it's incremented
}

for (int n : store)
System.out.print(n + " "); // output: [1 5 7 9 3 -2 4 2]
}

jejewite
Автор

int index = 0;
for (int i = 0; i < arr.length; i++)
if (arr[i] % 2 == 1) {
int temp = arr[index];
arr[index++] = arr[i];
arr[i] = temp;
}

ΝικήταςΚακούλλης
Автор

Thank you very much for such a superb explanation!

a.human.
Автор

you didnt hava to create seperate method to copy arrays, you could hava just done...
numbers = temp;

TroddenAnimations
Автор

I didn't know you could use '++' inside the objects parameter window and have it work outside of the statement, great demonstration of that trick. I was close but still successful.
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numbers = {2, 4, 5, 7, 3, 9, 6};
int[] numbArr = new int[numbers.length];
int j = 0;
for (int i = 0 ; i < numbers.length ; i++)
if (numbers[i] % 2 != 0) {
numbArr[j] = numbers[i];
j++;
}
for (int i = 0 ; i < numbers.length ; i++)
if (numbers[i] % 2 == 0) {
numbArr[j] = numbers[i];
j++;
}

}
}

dogchassingstars
Автор

Thank you so much
Please Complete this Java series.

dipeshjaiswal
Автор

This is my own logic, and it works correctly:

//let odd numbers be at the front and even numbers be at the back went print in array

import java.util.Arrays;
import java.util.Scanner;

public class ArrayExercise5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

//size of array
System.out.print("Enter the size of the array: ");
int size = input.nextInt();

int[] array = new int[size]; //array object

int i = 0, j = size-1;
int count = 0;

while(count < size){
System.out.print("Enter for elememt " + (count + 1) + ": ");
int nums = input.nextInt();

if(nums % 2 != 0){
array[i++] = nums;
}
else{
array[j--] = nums;
}

count++;
}
// print array
String arrayString = Arrays.toString(array);


input.close();
}
}

abdulrahimjalloh
Автор

Pls keep upload exercises in Java for beginners! You are the best!

mihaelaalbu
Автор

Please continue uploading everyday ... Iam waiting you ❤️

omarahmed
Автор

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = input.nextInt();
int[] arrays = new int[n];
fillArrays(arrays);
printArrays(arrays);
arrangedArrays(arrays);
}

private static void copyArrays(int[] temp, int[] arrays) {
for(int i = 0; i < arrays.length; i++){
arrays[i] = temp[i];
}

}

private static void arrangedArrays(int[] arrays) {
int j = 0;
int k = arrays.length - 1;
int[] temp = new int[arrays.length];
for (int i = 0; i < arrays.length; i++) {
if (arrays[i] % 2 != 0) {
temp[j] = arrays[i];
j++;
} else {
temp[k] = arrays[i];
k--;
}
}
copyArrays(temp, arrays);
}





private static void printArrays(int[] arrays) {

}

private static void fillArrays(int[] arrays) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the elements: ");
for(int i = 0; i < arrays.length; i++){
System.out.print("Element " + (i + 1) + ": ");
arrays[i] = input.nextInt();
}
}
}

caesaaar
Автор

but why void return type has changed the content of the array numbers[] is it not just a parameter ?

justoussamagaming
Автор

// can we write like this
class odd{
public static void main(String[] args){
int [] a={1, 8, 3, 12, 5, 74, -3, -4, 90, 80, 24, 73};
int i;
for(i=0;i<a.length;i++){
if(a[i]%2!=0)
{
System.out.println(a[i]);
}
}
for(i=0;i<a.length;i++){
if(a[i]%2==0)
{
System.out.println(a[i]);
}
}
}
}

praveennamburi
Автор

import java.util.Arrays;

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

int[] numbers= {1, 2, 4, 5, 7, 9, -2, 3};

int[] temp=new int[numbers.length];

int j=0;
int k=numbers.length - 1;

for(int i=0;i<numbers.length;i++)
if(numbers[i]%2!=0)
temp[j++]=numbers[i];
else
temp[k--]=numbers[i];


}
}
//Why to create copy....directly print the temp array

himanshuchauhan
Автор

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

Scanner num=new Scanner(System.in);
System.out.println("enter the nth number");
int n=num.nextInt();
int array[]=new int [n];
int temp[]=new int [array.length];
for (int i=0;i<array.length;i++)
{
array[i]=num.nextInt();

}

arrange(array, array);

}


public static void arrange (int [] array, int temp[])
{
int j=0;
int k=array.length-1;
for (int i=0;i<array.length;i++)
{
if (array[i]%2!=0)
{

}
else
{

}
for (int p=0;i<array.length;i++)
{

}

}
System.out.println("after rearrangeing array");




}

}

thivagarmurugan
Автор

but how to print even numbers in ascending order...

atomicnature
Автор

please teach us how to debug thank you

omarahmed
Автор

Can I reupload ur video (ToC) in hindi?

computerlibrary
Автор

public class Main {
public static void main(String[] args) {
int[] numbers={1, 2, 4, 5, 7, 9, -2, 3};
int[] temp=new temp[8];
for (int i=0; i<temp.length; i++) {
if (numbers[i]%2==1)
int temp[i]=numbers[i]
");
else
int
");
} 🤣
}
}

kellanagaraju