Let’s make some coding examples….

List Concatenation

Concatenation of lists is an operation where the elements of one list are added at the end of another list. For this we need to use + operator.

code 1:

void main(){
  var lst1=[1,2,3];
  var lst2=lst1+[50,60];
  print(lst2);
}
output

List merging

List merging means merge two strings and stores into another string.

Code 2:

void main(){
  var lst1=[10,20,30];
  var lst2=[-60,-75,-90];
   print(‘lst1 is: $lst1’);
  print(‘lst2 is: $lst2’);
  // merging lst1,lst2
  var finalLst=lst1+lst2;
  print(‘final list: $finalLst’);
}
output

code 3:

void main(){
  var str1=[‘debain’,’Ubuntu’,’linux mint’];
  var str2=[‘arch’,’garuda linux’,’fedora’];
  // merging two strings
  var finalStr=str1+str2;
  print(‘final string is -> $finalStr’);
}
output

We can perform various operations – searching, sorting (bubble), and arithmetic operations on list items.

Write a code, to print all the items of String list with indexes.

Code 4:

void main(){
  var lst=[‘linux’,’debian’,’windows’,’android’,’free BSD’];
  print(‘list is -> $lst’);
  print(‘list type ->’);
  print(lst.runtimeType);
  for(int i=0;i<lst.length;i++){
    print(‘index $i item ->’);print(lst[i]);
  }
}
output

Write a code, to perform addition operation on integer list items.

Code 5:

void main(){
  var lst=[10,15,20,25,40];
  print(‘list is -> $lst’);
  int sum=0,temp=0;
  for(int i=0;i<lst.length;i++){
    temp=lst[i];
    sum=sum+temp;
  }
  print(‘sum of all items -> $sum’);
}
output

 Searching

There are two types of searching techniques –

1. Linear search

2. Binary search

Let’s work with linear search

Write a code, in dart to perform linear search in String and double list both.

Code 6:

import ‘dart:io’;
 
void main(){
  var lst=[‘dipsayak’,’aditi’,’ramesh’,’arnab’,’shravani’];
  String? item;
  print(‘Enter String u search -> ‘);
  item=stdin.readLineSync();
 
  print(‘list is: $lst’);
  for (var i = 0; i < lst.length; i++) {
    if (lst[i]==item) {
      print(‘Item bound!’);
      break;
    }
  }
}
output

code 7:

import ‘dart:io’;
 
void main(){
  var lst=[1.2,3.5,6.7,7.9,9.3];
  String? item;double a=0;
 
  print(‘Enter String u search -> ‘);
  item=stdin.readLineSync();
  if(item!=null){
    a=double.parse(item);
  }
 
  print(‘list is: $lst’);
  for (var i = 0; i < lst.length; i++) {
    if (lst[i]==a) {
      print(‘Item bound!’);
      break;
    }
  }
}
output

Let’s work with binary search

Write a code, to perform binary search in list.

Code 8:

import ‘dart:io’;
 
void main(){
  var lst=[10,20,25,35,40,60,90];
  print(‘list is: $lst’);
 
  int mid=0,start=0,end=lst.length-1;
  late double a;int b=0;String? str;
 
  print(“Enter item u binary search: “);
  str=stdin.readLineSync();
  if (str!=null) {
   // typecasting string->integer
    b=int.parse(str);
  }
 
 for (var i = 0; i < lst.length; i++) {
  a=(start+end)/2;
  // converts double value -> integer
  mid=a.round();
  if(b==lst[mid]){
    print(‘item found!’);
    break;
  }
  else if(b>lst[mid]){
    start=mid++;
  }
  else if(b<lst[mid]){
    end=mid–;
  }
 
 }
 if(b==lst[0]){
    print(‘item found!’);
  }
}
output

Sorting

Sorting a technique which we can sort items into list. There are many sorting techniques –

  • Bubble sort
  • Merge sort
  • Radix sort
  • Insertion sort

Etc. We will discuss about only – bubble sort.

Write a code, to perform bubble sort in dart on integer list.

Code 9:

void main(){
  int temp=0;
  var arr=[50,70,65,35,-45,-9,23,45];
  print(‘before sort list was: $arr’);
 
  for(int i=0;i<arr.length;i++){
    for (var j = 0; j <arr.length-1; j++) {
      if(arr[j]>arr[j+1]){
        temp=arr[j+1];
        arr[j+1]=arr[j];
        arr[j]=temp;
      }
    }
  }
  print(‘after sort list is: $arr’);
}
output