Posts Tagged code

Simple Java Combinations Algorithm Code - Calculate Combinations of Elements in an Array

Below is some code I wrote recently to calculate the different combinations of elements in an Array. It is a simplified and straightforward recursive solution.

A recursive solution has 2 important piecies: a base case, and a recursive step. Here is an example of how the code below executes:

given: [1,2,3,4]

Recursive Step - Iterate through each element and break it out, leaving the rest of the elements
(first iteration)
1 [2,3,4]
2 [1,3,4]
3 [1,2,4]
4 [1,2,3]

(second iteration)

1 2 [3,4]
1 3 [2,4]
1 4 [2,3]

…. etc

The recursive step is defined in plain english as just remove a value, then call the function again on the rest of the list (with this element removed).

In the base case, once we have an array the size of length 1, we just simply return the array - so simple!

There is a key piece of code that, given an element and an array of arrays, prepends the element to each array. i.e.

1, [[5,6], [7,8], [3,4]] –> [ [1,5,6], [1,7,8], [1,3,4] ]

Pretty simple.

import java.util.ArrayList;

public class Combination {
public ArrayList<ArrayList<Object>> compute(ArrayList<Object> restOfVals) {
if(restOfVals.size() < 2) {
ArrayList<ArrayList<Object>> c = new ArrayList<ArrayList<Object>>();
c.add(restOfVals);
return c;
}
else {
ArrayList<ArrayList<Object>> newList = new ArrayList<ArrayList<Object>>();
for(Object o: restOfVals) {
//make a copy of the array
ArrayList<Object> rest = new ArrayList<Object>(restOfVals);
//remove the object
rest.remove(o);
newList.addAll(prependToEach(o, compute(rest)));
}
return newList;
}

}
private ArrayList<ArrayList<Object>> prependToEach(Object v, ArrayList<ArrayList<Object>> vals){
for(ArrayList<Object> o : vals) {
o.add(0, v);
}
return vals;
}
public static void main(String args[]) {
ArrayList<Object> i = new ArrayList<Object>();
i.add("1");
i.add("2");
i.add("3");
i.add("4");
Combination c = new Combination();
ArrayList<ArrayList<Object>> ff = new ArrayList<ArrayList<Object>>();
ff = c.compute(i);
System.out.println(ff.size());
System.out.println(ff);
}
}

Tags: , , , , ,

Android TabHost Basic Code Example

Here is a basic example of a TabHost, which allows a user to create a tabs view (the default android ‘contacts/dialer’ menu uses tabhost).

First, the code:

package com.android.mytabviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class tabViewTest2 extends Activity {

private int ADD_NEW_TAB = Menu.FIRST;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost);
tabs.setup();
TabSpec tspec1 = tabs.newTabSpec(”First Tab”);
tspec1.setIndicator(”One”);
tspec1.setContent(R.id.content);
tabs.addTab(tspec1);
TabSpec tspec2 = tabs.newTabSpec(”Second Tab”);
tspec2.setIndicator(”Two”);
tspec2.setContent(R.id.content);
tabs.addTab(tspec2);
TabSpec tspec3 = tabs.newTabSpec(”Third Tab”);
tspec3.setIndicator(”Three”);
tspec3.setContent(R.id.content);
tabs.addTab(tspec3);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0, ADD_NEW_TAB, 0, “New Tabs”);
return true;
}

//Dynamically delete tabs, then add one again
//Problem with SDK 1.1 returns null pointer exception
@Override
public boolean onOptionsItemSelected(MenuItem item) {

TabHost tabs = (TabHost) this.findViewById(R.id.my_tabhost);
tabs.clearAllTabs();
tabs.setup();
TabSpec tspec1 = tabs.newTabSpec(”New Tab”);
tspec1.setIndicator(”NEWTAB”, this.getResources().getDrawable(R.drawable.icon));
tspec1.setContent(R.id.content);
tabs.addTab(tspec1);

return super.onOptionsItemSelected(item);
}
}

Now, the XML (i.e. main.xml):

<?xml version=”1.0″ encoding=”utf-8″?>
<TabHost xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/my_tabhost”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TabWidget
android:id=”@android:id/tabs”
android:layout_width=”fill_parent”
android:layout_height=”65px”/>

<FrameLayout
android:id=”@android:id/tabcontent”
android:layout_width=”fill_parent”
android:layout_height=”200px”
android:paddingTop=”65px”>
<LinearLayout
android:id=”@+id/content”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
</LinearLayout>
</FrameLayout>
</TabHost>

For more information, follow this link to the documentation for a TabHost.

http://developer.android.com/reference/android/widget/TabHost.html

Tags: , , , , , , , ,

Basic Android Tutorial: Countdown Timer Code

Here is some basic code to make a timer that counts down.

You can specify the start value you want and the amount you want it to count down by.

package com.android.countdown;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

public class CountDownTest extends Activity {

TextView tv; //textview to display the countdown

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

tv = new TextView(this);
this.setContentView(tv);

//5000 is the starting number (in milliseconds)
//1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000,1000);

counter.start();

}

//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{

public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
tv.setText(”done!”);
}

@Override
public void onTick(long millisUntilFinished) {
tv.setText(”Left: ” + millisUntilFinished/1000);

}

}
}

Tags: , , , , , , , ,