Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Java Code

Uploaded by

Adil Khattab
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java Code

Uploaded by

Adil Khattab
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

/// Array/////////////////////////////////

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}

//////////////////////////////////////////////////////////////////////

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


for (String i : cars) {
System.out.println(i);
}

/////////////////////Multi-Dimensional
Array///////////////////////////////////////////

public class Main {


public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}

//////////////////Array List//////////////////////////////////////////////////////

public class Main {


public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (int i = 0; i < cars.size(); i++) {
System.out.println(cars.get(i));
}

////////////////////////Sorting array
list/////////////////////////////////////////////////////////////

ArrayList<String> cars = new ArrayList<String>();


cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars); // Sort cars
for (String i : cars) {
System.out.println(i);
}

///////////////////////HashMap//////////////////////////////////////////////////

public class Main {


public static void main(String[] args) {
// Create a HashMap object called people
HashMap<String, Integer> people = new HashMap<String, Integer>();

// Add keys and values (Name, Age)


people.put("John", 32);
people.put("Steve", 30);
people.put("Angie", 33);

for (String i : people.keySet()) {


System.out.println("key: " + i + " value: " + people.get(i));
}
}

//////////////////////
HashSet////////////////////////////////////////////////////////

A HashSet is a collection of items where every item is unique

public class Main {


public static void main(String[] args) {

// Create a HashSet object called numbers


HashSet<Integer> numbers = new HashSet<Integer>();

// Add values to the set


numbers.add(4);
numbers.add(7);
numbers.add(8);

// Show which numbers between 1 and 10 are in the set


for(int i = 1; i <= 10; i++) {
if(numbers.contains(i)) {
System.out.println(i + " was found in the set.");
} else {
System.out.println(i + " was not found in the set.");
}
}

//////////////////Access
Modifiers///////////////////////////////////////////////////

A)Access Modifiers

1) classes, you can use either public or default:


public : The class is accessible by any other class
default : The class is only accessible by classes in the same package. This is
used when you don't specify a modifier. You will learn more about packages in the
Packages chapter

2) For attributes, methods and constructors, you can use the one of the following:

Modifier Description
public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package. This is used when you
don't specify a modifier. You will learn more about packages in the Packages
chapter
protected The code is accessible in the same package and subclasses. You will
learn more about subclasses and superclasses in the Inheritance chapter

B) Non-Access Modifiers

1) For classes, you can use either final or abstract:

final : The class cannot be inherited by other classes (You will learn more
about inheritance in the Inheritance chapter)
abstract : The class cannot be used to create objects (To access an abstract
class, it must be inherited from another class. You will learn more about
inheritance and abstraction in the Inheritance and Abstraction chapters)

2) For attributes and methods, you can use the one of the following:

final Attributes and methods cannot be overridden/modified


static Attributes and methods belongs to the class, rather than an object
abstract Can only be used in an abstract class, and can only be used on methods.
The method does not have a body, for example abstract void run();. The body is
provided by the subclass (inherited from). You will learn more about inheritance
and abstraction in the Inheritance and Abstraction chapters

//////////////////////////////////////////////////////////////////

Java Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.

//////////////////Count
words//////////////////////////////////////////////////////////////////////////////
//////////////

String words = "One Two Three Four";


int countWords = words.split("\\s").length;
System.out.println(countWords);

///////////////////Reverse a
string ////////////////////////////////////////////////////

public class Main {


public static void main(String[] args) {
String originalStr = "Hello";
String reversedStr = "";
System.out.println("Original string: " + originalStr);

for (int i = 0; i < originalStr.length(); i++) {


reversedStr = originalStr.charAt(i) + reversedStr;
}
System.out.println("Reversed string: "+ reversedStr);
}
}

////////////////////Sum of an
Array/////////////////////////////////////////////////////////

int[] myArray = {1, 5, 10, 25};


int sum = 0;
int i;

// Loop through the array elements and store the sum in the sum variable
for (i = 0; i < myArray.length; i++) {
sum += myArray[i];
}

System.out.println("The sum is: " + sum);

///////////////Add 2 numbers/////////////////////////////////////////////////////

int x = 5;
int y = 6;
int sum = x + y;
System.out.println(sum); // Print the sum of x + y

//////////////////Iterator////////////////////////////////

public class Main {


public static void main(String[] args) {

// Make a collection
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

// Get the iterator


Iterator<String> it = cars.iterator();

// Print the first item


System.out.println(it.next());
}
}

/////////////////////////////////////////////
Java Variables

n Java, there are different types of variables, for example:

String ,int,float,char,boolean

/////////////////Count ///////////////////////////////////////////
String someString = "elephant";
char someChar = 'e';
int count = 0;

for (int i = 0; i < someString.length(); i++) {


if (someString.charAt(i) == someChar) {
count++;
}
}
assertEquals(2, count);

/////////////////////////////////////////////////////////////

How to count characters without white spaces in Java?

class CountNonWhiteSpaceCharacters {

public static void main(String[] args) {

String str = "This is a sample string";

int count = 0;

// count each characters except spaces

for(int i=0; i < str.length(); i++) {

if(str.charAt(i) != ' ')


count++;
}

System.out.println("Length of the String: " + count);

///////////////////////////////////////////////////////////
How to count the occurrence of a specific character in java?

class CountOccurencesOfCharacter {

public static void main(String args[]) {

String inputString = "Hello World";

char searchChar = 'l';

int count=0;
for(int i=0; i < inputString .length(); i++) {

if(inputString .charAt(i) == searchChar)


count++;
}

System.out.println("The Occurrence of Character ("+searchChar +") is:


"+count);
}
}
//////////////////////////////////////////////////////////////////////

How to count the occurrence of a specific character in java?

class CountOccurencesOfCharacter {

public static void main(String args[]) {

String inputString = "Hello World";

char searchChar = 'l';

int count=0;

for(int i=0; i < inputString .length(); i++) {

if(inputString .charAt(i) == searchChar)


count++;
}
System.out.println("The Occurrence of Character ("+searchChar +") is:
"+count);
}
}

////////////////////////////////////////////////////////////

How to efficiently count the occurrence of characters in java?

class OccurenceOfCharInString {

public static void main(String[] args)

String str = "Hello World";

HashMap countCharMap = new HashMap();

char[] strArray = str.toCharArray();

for (char c : strArray) {

if(countCharMap.containsKey(c)) {

countCharMap.put(c, countCharMap.get(c) + 1);


}
else {
countCharMap.put(c, 1);
}
}

for (Map.Entry entry : countCharMap.entrySet()) {

System.out.println(entry.getKey() + " " + entry.getValue());

}
}

///////////////////////////////////////////////////////////////////////////////////

You might also like