dimanche 19 avril 2015

Why Wrapper Class does not have pool Similar to Stringpool?

Integer, Character, Double all these are immutable class like String. String has Stringpool to save memory But why they dont have similar pool? I have checked Integer is having a similar pool only upto 127 value but not more than that.


Jquery Numberical Array to String and get a Substr()

I initiated a numerical array inside a javascript and to converted it into a string using join() but when I try to get a substr(), it doesn't to work. There seems to be a techincal error. Please help!



var array = [85, 13, 7, 42, 78, 9];
$("#div1").html("<b>This is the original array:</b><br><br>" + array.join("<br>"));

$("#div2").html("<br><b>This is the converted string:</b><br><br>" + array.join(""));

$("#div3").html("<br><b>The substring (from 0 to 3) is:</b><br><br>" + array.substr(0,3));


NOTE: div1,div2,div3 are 3 seperate divs with ids respectively. Thats where i want to display the results.


is there a way to get rid of spaces after pop_Back() c++?

I used pop back to get rid of a few things on a list now there is blank spaces making so the text things appear further down. is there a way to only use .pop_back(),.empty(), .clear(), .empty(), or .swap()? because i've tried all the ways i can think of just with these.


no code please, just words.


Istream to string conversion with \n characters in C++

how can I convert istream to string, when my istream also includes newline characters and I don't want to escape whitespaces? Thank you.


Different Outputs in Visual Studio and gcc in cygwin

Same code (Simulating a Shift Reduce Parser) gives different outputs in Visual Studio AND gcc (in cygwin). The problem seems to be in the last function where the strings are compared.


(memory leak maybe) After much debugging I found that gcc does not read E+E after accepting it is converted to some weird thing this(must be some ASCII value)->▒▒▒


gcc version: 4.8.3 Visual Studio Ultimate 2013 version: 12.0.30723


Code:



//shift reduce parser
#pragma warning (disable : 4996)
#include<stdio.h>
#include<malloc.h>
#include<string.h>
char stack[10];
int st = -1, n;
char *(*pro), *(*var);
void match(int);
int main()
{
int i;
char s[10];
printf("Enter the number of productions: ");
scanf("%d", &n);
pro = (char**)malloc(sizeof(char*));
var = (char**)malloc(sizeof(char*));
printf("Enter the productions on LEFT and RIGHT sides one at a time:\n");
for (i = 0; i < n; i++)
{
pro[i] = (char*)malloc(sizeof(char));
var[i] = (char*)malloc(sizeof(char));
scanf("%s", var[i]);
printf("->");
scanf("%s", pro[i]);
}
printf("Enter input string: ");
scanf("%s", s);
i = 0;
//Start processing
do
{
stack[++st] = s[i];
printf("%s\n", stack);
match(1);
i++;
} while (s[i] != '\0');


//reduction
i = 1;
while (st >= 0)
{
st--;
i++;
match(i);
printf("%s\n", stack);
}
if (stack[0] == var[0][0])
printf("Accepted");
while (1);
return 0;
}
//matches k characters of stack with productions
void match(int k)
{
int i;

for (i = 0; i < n; i++)
{
if (!(strncmp(&stack[st], pro[i], k)))
{
strncpy(&stack[st], var[i], k);
st -= k - 1; //tentative
i = -1;
if (k == 1)
{
return;
}
}
}
}


Visual Studio Output:



Enter the number of productions: 5
Enter the productions on LEFT and RIGHT sides one at a time:
E
->E+E
E
->E*E
E
->a
E
->b
E
->c
Enter input string: a+b*c
a
E+
E+b
E+E*
E+E*c
E+E*E
E
Accepted


gcc output



Enter the number of productions: 5
Enter the productions on LEFT and RIGHT sides one at a time:
E
->E+E
E
->E*E
E
->a
E
->b
E
->c
Enter input string: a+b*c
a
E+
E+b
E+E*
E+E*c
E+E*E
E+E
E+E


removing the pragma warning has no effect(it is there to use printf and scanf statements in VS)


PS. Iam sort of new here so if you are downvoting this question please specify the reason


Second Navigation Drawer different actions

I feel really dumb asking this but cant get it to work... I have a Navigation Drawer in my app and a second navigation drawer on the right side of my app i have gotten the icons and the names in it to be different than the first one... however the actions when they are clicked are the same ones that are in the left drawer... How to get it to work separately.


Here is my MainActivity where the actions for the first drawer are set.



@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}

public void onSectionAttached(int number) {
String[] stringArray = getResources().getStringArray(R.array.section_titles);
if (number >= 1) {
mTitle = stringArray[number - 1];
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
if(data==null){
webView.loadUrl("file:///android_asset/index.html"); } else {
data = null;
}
break;
case 2:
mTitle = getString(R.string.title_section2);
if (webView.canGoBack())
webView.goBack();
break;
case 3:
mTitle = getString(R.string.title_section3);
if (webView.canGoForward())
webView.goForward();
break;
case 4:
mTitle = getString(R.string.title_section4);
webView.reload();

break;
case 5:
mTitle = getString(R.string.title_section5);
if(getActionBar().isShowing()){
getActionBar().hide();
}else{
getActionBar().show();}
break;
}
}
}


Here is the NavigationDrawerFragment for first drawer



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);



mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
final TypedArray typedArray = getResources().obtainTypedArray(R.array.sections_icons);
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
getResources().getStringArray(R.array.sections)
) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
int resourceId = typedArray.getResourceId(position, 0);
Drawable drawable = getResources().getDrawable(resourceId);
((TextView) v).setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
return v;
}
});
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}


The strings.xml for both drawers



<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">MyApp</string>
<string name="title_section1">Home</string>
<string name="title_section2">Back</string>
<string name="title_section3">Forward</string>
<string name="title_section4">Refresh</string>
<string name="title_section5">Fullscreen</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>


<string-array name="sections">
<item></item>
<item></item>
<item></item>
<item></item>
<item></item>

</string-array>

<string-array name="section_titles">
<item>Section T1</item>
<item>Section T2</item>
<item>Section T3</item>
<item>Section T4</item>
<item>Section T5</item>

</string-array>


<string-array name="sections_icons">
<item>@drawable/ic_action_home</item>
<item>@drawable/ic_action_back</item>
<item>@drawable/ic_action_forward</item>
<item>@drawable/ic_action_refresh_enabled</item>
<item>@drawable/ic_action_full_screen1</item>

</string-array>
<string-array name="sections2">
<item>section ha</item>
<item></item>
<item></item>
<item></item>

</string-array>

<string-array name="section_titles2">
<item>Section T1</item>
<item>Section T2</item>
<item>Section T3</item>
<item>Section T4</item>

</string-array>
<string-array name="sections_icons2">
<item>@drawable/ic_launcher</item>
<item>@drawable/ic_launcher</item>
<item>@drawable/ic_launcher</item>
<item>@drawable/ic_launcher</item>

</string-array>

</resources>


And lastly the NavigationDrawerFragment2 for the right drawer that i need to change the actions



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer2, container, false);



mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
final TypedArray typedArray = getResources().obtainTypedArray(R.array.sections_icons2);
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
getResources().getStringArray(R.array.sections2)
) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
int resourceId = typedArray.getResourceId(position, 0);
Drawable drawable = getResources().getDrawable(resourceId);
((TextView) v).setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
return v;
}
});
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}

iOS (Swift): NSScanner string always nil (EXC_BAD_INSTRUCTION(code=EXC_i386_INVOP,subcode=0x0))

When using NSScanner, the following crashes:



var s: String?
var b: NSString?
let scn = NSScanner(string: "andgostring")
scn.scanUpToString("go", intoString: &b)
b = NSString(string:"http")
s = b // <--- Crashes here with EXC_BAD_INSTRUCTION(code=EXC_i386_INVOP,subcode=0x0


What's wrong here?