Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Java pass variable as reference


Integer, Float, String, List passed as reference in JAVA example source code:

// List passed as reference JAVA
List<Integer>list = new ArrayList<Integer>();

public void fc (List<Integer>listRef){
listRef.add(7);
listRef.add(5);
}
fc(list); // 7, 5 


// String passed as reference JAVA
	public void mutate(AtomicReference<Object> ref) { ref.set("Goodbye"); }

		AtomicReference<Object> ref = new AtomicReference<Object>("Hello");
		mutate(ref);
		System.out.println(ref.get()); //Goodbye!
             String s = (String) ref.get();


// Integer passed as reference JAVA
	private static void mutate(AtomicReference<Object> ref) { ref.set(7); }

	//public static void main(String[] arg) {
		AtomicReference<Object> ref = new AtomicReference<Object>(5);
		mutate(ref);
		System.out.println(ref.get()); // 7
int n = (Integer) ref.get();


// Float passed as reference JAVA
	private static void mutate(AtomicReference<Object> ref) { ref.set(14.8f); }

	//public static void main(String[] arg) {
		AtomicReference<Object> ref = new AtomicReference<Object>(12.1f);
		mutate(ref);
		float f = (Float) ref.get();
		System.out.println(f); //14.8


397LW NO topic_id



Další témata ....(Topics)


87

Map TreeMap key value pair sort by key, using of iterator Java Android example | map-treemap-key-value-pair-sort-by-key-using-of-iterator-java-android-example


Map TreeMap key value pair, Map sort by key, Iterator for Map Java Android example.

MainClass.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class MainClass {
	public static void main(String[] arg) {
		
		// english;germany dictionary
		String[] arrayOfString = { "one;eine", "two;zwei", "three;drei", "four;vier" };

		Map<String, String> map = new TreeMap<String, String>();
	    
		for(String s: arrayOfString){
	    	String[] array = s.split(";");
	    	String sKey ="", sValue="";
	    	if(array.length > 1){
	    	sKey = array[0]; sValue = array[1];
	    		map.put(sKey, sValue);
	    	}
	    }
		List sortedByKeys=new ArrayList(map.keySet());
		Collections.sort(sortedByKeys);
		
            // iterate map
	    Set references = map.keySet();
	    Iterator it = references.iterator();
	    while (it.hasNext()) {
	      String key = (String) it.next();
	      String value = map.get(key);
	      System.out.println(key + " = " + value);
	    }
            // or other example how iterate map
	    TreeSet<String> keys = new TreeSet<String>(map.keySet());
	    for (String key : keys) { 
	       String value = map.get(key);
	       System.out.println(key + " = " + value);
	       
	    }		


		
       // check if key exists 	    
//	   if( map.containsKey("two")){
//		System.out.print("two = " + map.get("two"));
//	   }
	}
}
/*
four = vier
one = eine
three = drei
two = zwei
 */
392

Android runtime change switch Activity theme | android-runtime-change-switch-activity-theme



static boolean mbThemeLight = false;

 @Override
    public void onCreate(Bundle savedInstanceState) {
         if(mbThemeLight)
        setTheme(android.R.style.Theme_Light);
        super.onCreate(savedInstanceState);
        // bla bla bla..........
    }

private void switchTheme(){
 mbThemeLight = true;
 this.recreate();
}
165

How to clear or hide an ImageView in Android | how-to-clear-or-hide-an-imageview-in-android



// solution 1
ImageView imageView = (ImageView)findViewById(R.id.myimage);
imageView.setImageDrawable(null);
// or
imageView.setImageResource(0);

// solution 2  hide ImageView
imageView.setVisibility(View.INVISIBLE);

// solution 3 resize ImageView 0, 0
 hImageViewSemafor.setLayoutParams(new LinearLayout.LayoutParams(0,0));
36

Titlebar title bar change text setTile Android example | titlebar-title-bar-change-text-android-example


Change the title associated with this activity. If this is a top-level activity, the title for its window will change. If it is an embedded activity, the parent can do whatever it wants with it.




 String sTitle = "My new title";
 setTitle(sTitle);



381

InterstitialAd Admob not visible | interstitialad-admob-not-visible



// on bottom of onCreate add listener
mInterstitialAd .setAdListener(new AdListener(){
     public void onAdLoaded(){
          interstitial.show();
     }
});

} // end onCreate

 public void displayInterstitial (){
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }





Editace: 2012-07-30 11:04:10
Počet článků v kategorii: 397
Url:java-pass-variable-as-reference
Share
Share
Tweet