Categories
Go

array in go lang

In this post, we will learn about array in go lang

In my previous tutorial, I have explained the installation of Go in windows. Please check out the below URL for that.
https://beginnersbug.com/install-go-lang-in-windows/

Arrays are basic in all programming languages which is used for data operations. We can have a collection of elements here.

Creating array in Go lang

Similar to other programming languages, we can create arrays with length and without length.

In the below example I created two arrays with indexes of 5 & 3 with the datatype of int and string

package main

// fmt package for user input & output
import "fmt"

// Main Method
func main() {
	// Creating an int array with index of 5
	var intarr = [5]int{10, 4, 6, 2, 12}
	// Creating an string array with index of 3
	var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
	// Printing arrays
	fmt.Println(intarr)
	fmt.Println(strarr)
}

Output

[10 4 6 2 12]
[Iron Man Hulk Thor]

Printing index of array

In the below example, we will print the element based on the index value.
Here we are printing element at index intarr(1) & strarr(2)

package main

// fmt package for user input & output
import "fmt"

// Main Method
func main() {
	// Creating an int array with index of 5
	var intarr = [5]int{10, 4, 6, 2, 12}
	// Creating an string array with index of 3
	var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
	// Printing arrays
	fmt.Println(intarr)
	fmt.Println(strarr)

	// Printing index of an array
	fmt.Println(intarr[1])
	fmt.Println(strarr[2])
}

Output

[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor

The below snippet will show you to print the length of an array.
len() method is used to print the length of the array

package main

// fmt package for user input & output
import "fmt"

// Main Method
func main() {
	// Creating an int array with index of 5
	var intarr = [5]int{10, 4, 6, 2, 12}
	// Creating an string array with index of 3
	var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
	// Printing arrays
	fmt.Println(intarr)
	fmt.Println(strarr)

	// Printing index of an array
	fmt.Println(intarr[1])
	fmt.Println(strarr[2])

	fmt.Println("Lenght of int array:", len(intarr))
	fmt.Println("Lenght of string array:", len(strarr))
}

Output

[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor
Lenght of int array: 5
Lenght of string array: 3

Iterate array using for loop

Here we are printing each and every element with the help of for loop

package main

// fmt package for user input & output
import "fmt"

// Main Method
func main() {
	// Creating an int array with index of 5
	var intarr = [5]int{10, 4, 6, 2, 12}
	// Creating an string array with index of 3
	var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
	// Printing arrays
	fmt.Println(intarr)
	fmt.Println(strarr)

	// Printing index of an array
	fmt.Println(intarr[1])
	fmt.Println(strarr[2])

	fmt.Println("Lenght of int array:", len(intarr))
	fmt.Println("Lenght of string array:", len(strarr))

	// Iterating array using for loop
	for i := 1; i < len(intarr); i++ {
		fmt.Print(intarr[i], " ")
	}
	fmt.Println()
	// Iterating array using for loop
	for i := 0; i < len(strarr); i++ {
		fmt.Print(strarr[i], " ")
	}

}

Output

[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor
Lenght of int array: 5
Lenght of string array: 3
4 6 2 12 
Iron Man Hulk Thor

Go lang play

You can play with the array using their playgrounds also https://go.dev/tour/moretypes/6

Categories
collections java

join two arraylist in java with example

In this post, we will learn to join two arraylist in java with example

ArrayList is a dynamic array concept in java. We use ArrayList in so many places.

Syntax
list1.addAll(list2);

From the above syntax list2 will added to list1.

Example
import java.util.ArrayList;
import java.util.List;

public class Join2List {

	public static void main(String[] args) {
		try {
			List<String> list1 = new ArrayList<String>();
			List<String> list2 = new ArrayList<String>();

			list1.add("Rajesh");
			list1.add("Usha");

			list2.add("Kumar");
			list2.add("Nandhini");

			System.out.println("---List1 value before adding list2---");
			for (String string : list1) {
				System.out.println(string);
			}

			// Adding list2 to list1
			list1.addAll(list2);

			System.out.println("---List1 value After adding list2---");

			for (String string : list1) {
				System.out.println(string);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
Output
---List1 value before adding list2---
Rajesh
Usha
---List1 value After adding list2---
Rajesh
Usha
Kumar
Nandhini
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/list/Join2List.java

Related Articles

ArrayList in java with example

Categories
collections java

convert iterator to list in java

In this tutorial, we will learn convert iterator to list in java

Here we are giving two approach to convert iterator to list in java

Syntax in Java 8
ArrayList<String> list = new ArrayList<String>();
iterator.forEachRemaining(list::add);
Syntax in Java 7
ArrayList<String> list = new ArrayList<String>();
while (iterator.hasNext()) {
  String string = (String) iterator.next();
  list.add(string);
}
Example
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class IteratorToList {

	public static void main(String[] args) {

		Iterator<String> iterator = Arrays.asList("Rajesh", "Kumar", "Beginners", "Bug").iterator();
		convertToListJava7(iterator);
		convertToListJava8(iterator);

	}

	/**
	 * Java 7 Approach
	 * 
	 * @param iterator
	 * @return
	 */
	public static ArrayList<String> convertToListJava7(Iterator<String> iterator) {
		ArrayList<String> list = new ArrayList<String>();
		while (iterator.hasNext()) {
			String string = (String) iterator.next();
			list.add(string);
		}

		return list;
	}

	/**
	 * Java 8 Apporach
	 * 
	 * @param iterator
	 * @return
	 */
	public static ArrayList<String> convertToListJava8(Iterator<String> iterator) {
		ArrayList<String> list = new ArrayList<String>();
		iterator.forEachRemaining(list::add);
		return list;
	}

}
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/list/IteratorToList.java

Related Articles

Iterate list using streams in java

Categories
collections java

foreach in java8 using streams

In this tutorial, we will learn about foreach in java8 using streams

In java8, We can easily iterate a list using streams.

Streams API provided a lot of features. In this post, we are going to see about foreach

Syntax
stream().forEach({operations});
Example

import java.util.ArrayList;
import java.util.List;

public class ForEachExample {

	public static void main(String[] args) {
		try {

			List studentList = new ArrayList();
			Student student = null;

			student = new Student();
			student.setId(1);
			student.setName("Rajesh");
			student.setAddress("Mumbai");
			studentList.add(student);

			student = new Student();
			student.setId(1);
			student.setName("Kumar");
			student.setAddress("Chennai");
			studentList.add(student);

			studentList.stream()
					.forEach(x -> System.out.println("Name is " + x.getName() + " Address is " + x.getAddress()));

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
Student.java

public class Student {

	private int id;
	private String name;
	private String address;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}
Output
Name is Rajesh Address is Mumbai
Name is Kumar Address is Chennai
Related Article

sorting operations in java using streams

Iterate list using streams in java

Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/ForEachExampleUsingStreams.java

Categories
collections java

Sort an Arraylist in java with example

In this tutorial, we will learn how to Sort an Arraylist in java with example by ascending order using collections class

If you are new to array list refer below link to understand array list
https://beginnersbug.com/arraylist-in-java/

Features

  • Sort the specified list into ascending order,
  • This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
  • The specified list must be modifiable, but need not be resizable.

Syntax

Collections.sort(list);

Example

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortArrayListExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList();
        list.add("Car");
        list.add("Zebra");
        list.add("Apple");
        list.add("Ball");
        System.out.println("Array list values before sorting \n");
        for (String string : list) {
            System.out.println("Value is " + string);
        }
        // Below sort method will sort your list
        Collections.sort(list);
        System.out.println("\nArray list values after sorting \n");
        for (String string : list) {
            System.out.println("Value is " + string);
        }
    }
}

Output

Array list values before sorting 

Value is Car
Value is Zebra
Value is Apple
Value is Ball

Array list values after sorting 

Value is Apple
Value is Ball
Value is Car
Value is Zebra

Reference

https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

Related Articles

iterate stream with index in Java8

foreach in java8 using streams

Categories
collections java

ArrayList in java with example

 

In this tutorial we will learn ArrayList in java with example 

In Java we can achieve the dynamic array using arraylist.
ArrayList class available on util package, so we don’t need to import any extra packages, In below example 

  • created new arraylist
  • added value to arraylist
  • printing the array list value by using for loop 

Example

import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {
	  public static void main(String[] args) {
      List arrayList = new ArrayList();
      arrayList.add("Java");
      arrayList.add("Java 8");
      arrayList.add("Java 11");
      for (int i = 0; i < arrayList.size(); i++) {
    System.out.println("Value at the index of " + i + " "+arrayList.get(i));
     }
   }
}

Output

Value at the index of 0 Java
Value at the index of 1 Java 8
Value at the index of 2 Java 11
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/ArrayListExample.java

Related Articles

Sort an Arraylist in java with example

iterate stream with index in Java8