2012년 11월 22일 목요일

arraylist remove doesn't work

check if you put Integer in the remove method, not int. Arraylist considers Integer as object.

How to sort ArrayList, removing duplicate items

 ArrayList arrayList1 = new ArrayList();
    
  
    
  //Create a HashSet which allows no duplicates
  HashSet hashSet = new HashSet(arrayList1);

  //Assign the HashSet to a new ArrayList
  ArrayList arrayList2 = new ArrayList(hashSet) ;
    
  //Ensure correct order, since HashSet doesn't
  Collections.sort(arrayList2);
    
  for (Object item : arrayList2)
    System.out.println(item);

2012년 11월 21일 수요일

How to disable visual effects and compiz 100%

Type "metacity --replace &" and hit enter on terminal.

Eclipse hangs on loading workbench


Try the following:
  1. Delete your .eclipse directory in your home directory. Launch eclipse. If that doesn't work,
  2. Open eclipse under another user account. If it loads, you know the problem is with your account, not your eclipse installation.
  3. If #2 didn't work your workspace might be screwed up. Delete the .metadata folder in your localworkspace (this is what worked for me). It seems that it contains a .LOCK file that if not properly closed, prevents eclipse from starting properly.

how to install java 1.7 on ubuntu 10.04

Type the following command on terminal.



sudo apt-get install oracle-java7-installer

startActivityForResult


From your FirstActivity call the SecondActivity using startActivityForresult() method
eg:
Intent i = new Intent(this, SeconActivity.class);
startActivityForResult(i, 1);
In your SecondActivity set the data which you want to return back to FirstActivity, If you don't want to return back don't set any.
eg: In secondActivity if you want to send back data
 Intent returnIntent = new Intent();
 returnIntent.putExtra("result",result);
 setResult(RESULT_OK,returnIntent);     
 finish();
if you don't want to return data
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();
Now in your FirstActivity class write following code for onActivityResult() method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {

     if(resultCode == RESULT_OK){

      String result=data.getStringExtra("result");

}

if (resultCode == RESULT_CANCELED) {

     //Write your code on no result return 

}
}//onAcrivityResult


Save ArrayList to SharedPreferences


//save the task list to preference
        SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
        Editor editor = prefs.edit();
        try {
            editor.putString(TASKS, ObjectSerializer.serialize(currentTasks));
        } catch (IOException e) {
            e.printStackTrace();
        }
        editor.commit();




//      load tasks from preference
        SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);

        try {
            currentTasks = (ArrayList<task>) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(new ArrayList<task>())));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }





You can get ObjectSerializer class from Apache Pig project ObjectSerializer.java

The following is OjectSerializer that I modified.

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kr.co.ht.smartsales.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.apache.commons.logging.Log;

public class ObjectSerializer {

    
    public static String serialize(Serializable obj) throws IOException {
        if (obj == null) return "";
        try {
            ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
            ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
            objStream.writeObject(obj);
            objStream.close();
            return encodeBytes(serialObj.toByteArray());
        } catch (Exception e) {
        }
  return null;
    }
    
    public static Object deserialize(String str) throws IOException {
        if (str == null || str.length() == 0) return null;
        try {
            ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
            ObjectInputStream objStream = new ObjectInputStream(serialObj);
            return objStream.readObject();
        } catch (Exception e) {
        }
  return str;
    }
    
    public static String encodeBytes(byte[] bytes) {
        StringBuffer strBuf = new StringBuffer();
    
        for (int i = 0; i < bytes.length; i++) {
            strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
            strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
        }
        
        return strBuf.toString();
    }
    
    public static byte[] decodeBytes(String str) {
        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length(); i+=2) {
            char c = str.charAt(i);
            bytes[i/2] = (byte) ((c - 'a') << 4);
            c = str.charAt(i+1);
            bytes[i/2] += (c - 'a');
        }
        return bytes;
    }

}