Reading file data into an array in JAVA JAVA by Ravinder Nath Rajotiya - April 18, 20210 Reading Numeric data from file into an array In this post we look out how we can integer read data from a file into an array. For this we need the following steps. Step-1: Create an array to store data read from file Step-2: create file object of class file and connect to file Step-3: Check if the file exist Step-4: Create object of Scanner class and make connection to input file using file object Step-5: Check if the current line has data in it using hasNext() Step-6: read from the line and set the pointer to begining of next line using object.nextLine() Step-7: close file when done Step-8: Display / process data read from the file Example reading from a file into an array package com.example; import java.io.IOException; public class arrays { public static
Copying Arrays in JAVA JAVA by Ravinder Nath Rajotiya - April 17, 2021April 17, 20210 Copy Array in JAVA In this lecturewe are going to see yet another impotant concep in arrays that is copying two arrays or copying one array into another. let us consider an array int []array1 = {2,4,6,8,10,15}; int []array2 =array1; //copy memory location of array1 into array2, so both array point to same location. and thus both array will have sae values. But, remember this is not copying array. this is copying address of on into another. It does not copy values. You can now print these if you print the two by comparing int []array1={2,4,6,8,10}; //declare an array with five elements int []array2=array1; //assign memory location of one into another, so both point to same location printArray(array1); //call to print array values printArra(array2); //call to print array
Comparing Arrays in JAVA JAVA by Ravinder Nath Rajotiya - April 17, 2021April 17, 20210 Comparing Arrays When we create array objects, they are created in memory at some memory address. Each array that is created is assigned different address. So, when we compare the array objects with == operators, their memory location get compared. So, they will never be equal even when they have the same values assigned to them. Same is the case when we compared string objects. Wrong way of comparing / Common mistake / Never compare arrays with == package com.example; public class arrays { public static void main(String[] args) { int []numbers1 ={2,4,6,8,10}; int []numbers2 ={2,4,6,8,10}; if (numbers1 == numbers2) { System.out.println("they are the same"); } else { System.out.println("they are not the same"); } } } So, we cannot compare the two array objects with == operators, because that will compare the memory
Arrays in JAVA JAVA by Ravinder Nath Rajotiya - April 17, 20210 arrays IN java An array is a container object that holds fixed number of values of single type. An array is used to store collection of data of same type. A link to an array is established after an array is created after creation the link is fixed. Syntax: int X []; or int []X; both mean the same int []numbers =new int[10]; // declare numbers as an array that can store 10 elements, and then assign values of same types at index values 0,1,2,3.... as: int number[0]=5; int number[1]= 10; int number[2]=15;...and so on position or index for array are from 0,1,2... up till 9, making size of array as 10. So we cannot write the value at index 10 because that
Methods in JAVA JAVA by Ravinder Nath Rajotiya - April 16, 20210 Methods in JAVA methods in JAVA add modularity and allows code reusability at different place or in modules. It is a very easy way of handling the complete bigger task. A method has basically two parts: a. Call to the method with correct type and number of arguments b. Method definitin which may or may not return values. The syntax of a method definition in JAVA is: public static void metodeName(data type arg(s) { local declaration of variable(s); statement(s); } Example-1: calling various methods for integer and string processing. Methods have been properly documented to aid in reading and understanding. package com.example; public class methosJAVA { public static void main(String[] args) { average(20, 35); double average=getAverage(35, 57); System.out.println("In main the average="+average); //System.out.println(); String fulName=fullName("Ram","Shyam" ); System.out.println(fulName); } /** * // this method calculates average but does not return * @param value1